我使用的是Rails3和最新版本的Devise,并且我在AdminController to authenticate_user!中有一个before过滤器。我需要在request.referrer重定向之前为它存储一个会话变量,这样当用户试图访问它时,我就可以将它发送回/admin页面。我应该在哪里重写authenticate_user!?
我想做的是这样,但我不知道在哪里定义它:
def authenticate_user!
session[:return_to] = request.request_uri
super
end发布于 2013-05-23 22:34:31
实际上你并不需要这样做,devise会为了这个目的而尊重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来自devise helper的:
# 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)发布于 2015-04-29 14:46:15
与Matt的答案不同的是,它不需要在每个页面查看时都记录返回页面:
在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
endhttps://stackoverflow.com/questions/16716679
复制相似问题