【问题标题】:cancancan + devise: handling timeoutable exceptioncancancan + 设计:处理超时异常
【发布时间】:2014-11-21 11:49:45
【问题描述】:

在我的 rails 3 应用程序中,我使用的是 devise 3.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 异常您无权访问此页面,但我想捕获一个可超时的 Devise(我猜)异常,以便我可以为此显示默认设计消息:您的会话已过期。请重新登录以继续

有什么想法吗?

【问题讨论】:

  • 我也有同样的问题...

标签: ruby-on-rails ruby-on-rails-3 devise cancancan


【解决方案1】:

当会话超时时,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

【讨论】:

    猜你喜欢
    • 2011-07-02
    • 1970-01-01
    • 2017-01-24
    • 2011-02-17
    • 2015-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多