2

In my rails 3 app I'm using devise 3.2.4 and cancancan 1.9.2. My problem is about the timeoutable devise module which works fine but I cannot rescue the right exception so that I can display the correct notice to the user.

my application_controller.rb contains:

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

Whenever the session expires I can rescue a generic CanCan::AccessDenied exception with message You are not authorized to access this page but I'd like to catch a timeoutable Devise (I guess) exception so that I can show the default devise message for that: Your session expired. Please sign in again to continue

Any idea?

4

1 に答える 1

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
于 2014-11-26T23:56:32.710 に答える