簡體   English   中英

cancancan + devise:處理超時異常

[英]cancancan + devise: handling timeoutable exception

在我的Rails 3應用程序中,我使用的是devise 3.2.4和cancancan 1.9.2。 我的問題是關於超時的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異常, 您無權訪問此頁面,但我想捕獲一個超時的Devise(我想)異常,以便為該事件顯示默認的devise消息。 : 您的會話已過期。 請再次登錄以繼續

任何想法?

會話超時時, 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM