1

我正在使用 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进入索引页面的方法。我做错了什么?

4

1 回答 1

1

我知道已经晚了,但我通过将 session["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 将在登录后重定向到 session["user_return_to"]。

于 2015-07-08T15:02:11.900 回答