2014-11-24 3 views
2

내 사용자 모델에 admin:boolean 필드가 있는데 내 컨트롤러에서 사용자가 관리자인지 확인하기 전에 필드를 편집 할 수 있기를 원합니다.Modyfying before_action

사용자가 관리자인지 확인하려면 before_action :authenticate_user!, only: [:edit]을 어떻게 수정합니까?

+0

가능한 중복 http://stackoverflow.com/questions/6425231/i-want-to-override-authenticate-user - 및 - 현재 - 사용자 - 방법 -가 - 보석 - 보석) –

답변

5

authenticate_user 이후에 호출 할 작업 전에 다른 것을 추가 할 수 있습니다! 현재 사용자에게 관리자 권한이 있는지 확인합니다.

class YourController 
    # first call authenticate_user! to check if user is signed in 
    before_action authenticate_user!, only: [:edit] 
    # if user is signed (current_user exist), check if he is admin 
    before_action authenticate_admin!, only: [:edit] 

    def authenticate_admin! 
    # check if current user is admin 
    unless current_user.admin 
     # if current_user is not admin redirect to some route 
     redirect_to 'some_public_route' 
    end 
    # if current_user is admin he will proceed to edit action 
    end 
end 
([I가 인증의 \의 _user 및 유증 보석의 현재 \의 _user 메소드를 오버라이드 (override) 할]의
관련 문제