在我的rails 3应用程序中,我使用的是Design3.2.4和cancancan 1.9.2。我的问题是可超时的设计模块,它工作良好,但我不能挽救正确的异常,以便我可以显示正确的通知给用户。
我的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异常,但我希望捕获一个可超时的设计(我猜)异常,以便显示默认的设计消息:您的会话过期了。请再次登录继续
有什么想法吗?
发布于 2014-11-26 23:56:32
当会话超时时,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
endhttps://stackoverflow.com/questions/27060722
复制相似问题