我正在使用 Cancan 和 Devise。我有以下内容,因此当 cancan 错误将页面重定向到登录路径时:
check_authorization :unless => :devise_controller?
rescue_from CanCan::AccessDenied do |exception|
redirect_to new_user_session_path, :alert => exception.message
end
效果很好。但是,一旦用户登录,我希望能够重定向回受 Cancan 保护的页面。
我在这里使用设计 wiki 中的脚本(如何:成功登录时重定向到特定页面)。
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
进入索引页面的方法。我做错了什么?