2013-06-12 73 views
1

我正在使用康康和設計。我有以下所以當cancan錯誤重定向頁面登錄路徑:設計:登錄後重定向回到cancan保護路徑

check_authorization :unless => :devise_controller? 
    rescue_from CanCan::AccessDenied do |exception| 
    redirect_to new_user_session_path, :alert => exception.message 
    end 

這很好。但是,我希望能夠在用戶登錄後將其重定向回Cancan保護的頁面。

我在此處使用Devise wiki中的腳本(How To: redirect to a specific page on successful sign in)。

def after_sign_in_path_for(resource) 
    sign_in_url = url_for(:action => 'new', :controller => 'sessions', :only_path => false, :protocol => 'http') 
    if request.referer == sign_in_url 
     # this path is used 
     super 
    else 
     stored_location_for(resource) || request.referer || root_path 
    end 
    end 

但這總是會使用super方法,都到索引頁。我做錯了什麼?

回答

1

我知道這是晚了,但我解決它加入會話[「user_return_to」]來rescue_from

check_authorization :unless => :devise_controller? 
rescue_from CanCan::AccessDenied do |exception| 
    session["user_return_to"] = request.fullpath 
    redirect_to new_user_session_path, :alert => exception.message 
end 

和去除after_sign_in_path_for方法重寫。然後,登錄後,Devise將重定向到會話[「user_return_to」]。

+0

更常用的解決方法是:http://stackoverflow.com/questions/26543503/redirect-back-to-current-page-using-omniauth -and-色器件 – lulalala

相關問題