简体   繁体   English

为什么request.referrer在Devise中对after_sign_out_for有效,但在after_sign_in中无效

[英]Why does request.referrer work for after_sign_out_for in Devise, but not after_sign_in

I have this in my application_controller.rb 我的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

It works perfectly on the sign out. 它在登出时效果很好。 It sends the user back to the same page they were on. 它将用户返回到他们所在的页面。

I would like to do the reverse for the sign in, but it gives me an infinite loop on the sign in. 我想对登录进行相反的操作,但它使我在登录时无限循环。

Why does this not work both ways? 为什么这两种方式都不起作用?

Edit 1 : To be precise, I get a Chrome, on OS X, message that says this: 编辑1 :确切地说,我在OS X上得到了一个Chrome消息,内容如下:

This webpage has a redirect loop

this would only make sense if request.referrer points to some action that tries to do another login. 仅当request.referrer指向尝试执行另一次登录的操作时,这才有意义。

you can find out about that by always having a terminal window open running tail -f log/development.log . 您可以通过始终打开终端窗口并运行tail -f log/development.log来了解有关信息。 all the information that you need should be in there. 您需要的所有信息都应该放在其中。 where does the request come from, where does it redirect to, why is there another redirect. 请求来自何处,重定向到何处,为什么还有另一个重定向。

anyways, there are other ways of sending a user back to it's origin. 无论如何,还有其他方法可以将用户送回原籍。 request.referrer is an optional header and often comes with a hassle in testing. request.referrer是一个可选的标头,通常在测试中带来麻烦。 devise itself offers different ways of customizing the after_sign_in_path_for methods. after_sign_in_path_for本身提供了不同的方式来定制after_sign_in_path_for方法。

read the method documentation: 阅读方法文档:

  # 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM