0

我有这个在我的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:确切地说,我在 OS X 上收到了一个 Chrome 消息,上面写着:

This webpage has a redirect loop

4

1 回答 1

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
于 2013-10-14T13:05:36.437 回答