2014-11-21 2 views
2

내 레일 3 앱에서 저는 devise 3.2.4와 cancancan 1.9.2를 사용하고 있습니다. 내 문제는 잘 작동 timeoutable devise 모듈에 대한 있지만 사용자에게 올바른 알림을 표시 할 수 있도록 올바른 예외를 구출 할 수 없습니다.cancancan + devise : 처리 시간 초과 가능 예외

application_controller.rb 다음이 포함

rescue_from Exception do |exception| 
    unless Rails.env.production? 
    raise exception 
    else 
    redirect_to :back, :flash => { :error => exception.message } 
    end 
end 

# https://github.com/ryanb/cancan/wiki/exception-handling 
rescue_from CanCan::AccessDenied do |exception| 
    flash[:error] = exception.message 
    respond_to do |wants| 
    wants.html { redirect_to main_app.root_url } 
    wants.js { render :file => "shared/update_flash_messages" } 
    end 
end 

세션이 나는 메시지 와 일반 CanCan::AccessDenied 예외를 구출 할 수 만료 할 때마다이 페이지에 액세스 할 수있는 권한이 없습니다하지만 난 잡을 싶습니다 timeoutable Devise (나는 짐작할 수있다.) 예외로, default devise 메시지를 표시 할 수있다 : 세션이 만료되었다. 계속하려면 다시 로그인하십시오.

아이디어가 있으십니까?

+0

나는 똑같은 문제가 있습니다. –

답변

0

세션이 시간 초과 될 때 flash[:alert]의 값은 :timeout으로 설정되며, 기본값은 config/locales/devise.en.yml입니다.

exception에서 메시지를 읽는 대신 flash[:alert]에서 읽어보고 그에 따라 반응하도록 만드십시오. 예를 들어, 내 앱에서 사용하는 코드입니다.

rescue_from CanCan::AccessDenied do |exception| 
    if user_signed_in? 
    # Blank page with an error message 
    flash.now.alert = exception.message 
    render text: '', layout: true, status: 403 
    else 
    # Redirect to login screen and display the message 
    redirect_to new_user_session_path, :notice => flash[:alert] || "You must login first" 
    end 
end 
관련 문제