我在application_controller.rb
def after_sign_in_path_for(resource)
request.referrer
end
def after_sign_out_path_for(resource_or_scope)
request.referrer
end
它完全适用于退出。它将用户发回到他们所在的同一页面。
我想对登录做相反的操作,但它会让我在登录时无限循环。
为什么这两种方式都不起作用?
编辑1 :准确地说,我在操作系统X上收到一条Chrome,上面写着:
This webpage has a redirect loop
答案 0 :(得分:0)
只有当request.referrer
指向某个尝试再次登录的操作时才会有意义。
您可以通过始终打开运行tail -f log/development.log
的终端窗口来了解相关信息。您需要的所有信息都应该在那里。请求来自哪里,它重定向到哪里,为什么还有另一个重定向。
无论如何,还有其他方法可以将用户发送回原点。 request.referrer
是一个可选的标题,通常会在测试中遇到麻烦。设计本身提供了自定义after_sign_in_path_for
方法的不同方法。
阅读方法文档:
# 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.
#
# By default, it first tries to find a valid resource_return_to key in the
# session, then it fallbacks to resource_root_path, otherwise it uses the
# root path. For a user scope, you can define the default url in
# the following way:
#
# map.user_root '/users', :controller => 'users' # creates user_root_path
#
# map.namespace :user do |user|
# user.root :controller => 'users' # creates user_root_path
# end
#
# If the resource root path is not defined, root_path is used. However,
# if this default is not enough, you can customize it, for example:
#
# def after_sign_in_path_for(resource)
# stored_location_for(resource) ||
# if resource.is_a?(User) && resource.can_publish?
# publisher_url
# else
# signed_in_root_path(resource)
# end
# end
#
def after_sign_in_path_for(resource_or_scope)
stored_location_for(resource_or_scope) || signed_in_root_path(resource_or_scope)
end