3

我在 Rails 3 和最新版本的 Devise 上,我有一个 before 过滤器我AdminController需要在它重定向之前authenticate_user!存储一个会话变量,request.referrer以便我可以在他们尝试时将其发送回 /admin 页面去在上面。我会在哪里覆盖authenticate_user!

我想做的是这个,但我不知道在哪里定义它:

def authenticate_user!
  session[:return_to] = request.request_uri
  super
end
4

2 回答 2

3

你实际上并不需要这样做,设计将尊重after_sign_in_path这个确切的目的。

在您的应用程序控制器中:

before_filter :set_return_path 

def after_sign_in_path_for(resource) 
  session["user_return_to"] || root_url 
end

def set_return_path
  unless devise_controller? || request.xhr? || !request.get?
    session["user_return_to"] = request.url
  end
end

从设计助手:

# The default url to be used after signing in. This is used by all Devise
# controllers and you can overwrite it in your ApplicationController to
# provide a custom hook for a custom resource.
# def after_sign_in_path_for(resource_or_scope)
于 2013-05-23T14:34:30.647 回答
-1

马特的答案的替代方案,它不需要在每次页面查看时记录返回页面:

在 application_controller.rb 中:

# Only remembers the page immediately before we 
# redirect them for auth, instead of every page view

def authenticate_user!
  session["user_return_to"] = request.fullpath
  super
end

# Deletes the return path as soon as it's used, so 
# they aren't accidentally redirected back again
# next time they login

def after_sign_in_path_for(resource)
  session.delete("user_return_to") || root_path
end
于 2015-04-29T06:46:14.597 回答