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?
When the session times out, the value of
flash[:alert]
is set to:timeout
, which by default is defined inconfig/locales/devise.en.yml
.So instead of reading the message from
exception
, try reading fromflash[:alert]
and make your app react accordingly. For example, this is the code I use in my apps: