当引用不可用时,在 Ruby on Rails 中正确执行 redirect_to :back

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/771656/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 21:10:18  来源:igfitidea点击:

Correctly doing redirect_to :back in Ruby on Rails when referrer is not available

ruby-on-railsrubyhttp

提问by user67050

I'm having a problem with redirect_to :back. Yes, it's referrers.

我有问题redirect_to :back。是的,是推荐人。

I often get the exception

我经常得到例外

(ActionController::RedirectBackError) "No HTTP_REFERER was set in the request to this action, so redirect_to :back could not be called successfully. If this is a test, make sure to specify request.env[\"HTTP_REFERER\"]."

(ActionController::RedirectBackError) “在此操作的请求中没有设置 HTTP_REFERER,因此无法成功调用 redirect_to :back。如果这是一个测试,请确保指定 request.env[\"HTTP_REFERER\"]。

I realize that this is a result of a referrer not being available. Is there a way that, for example, one can set a session variable on each access with the last page visited, and, when HTTP_REFERER is not available, utilize this session variable to redirect to?

我意识到这是推荐人不可用的结果。例如,有没有一种方法可以在每次访问上次访问的页面时设置会话变量,并且当 HTTP_REFERER 不可用时,利用此会话变量重定向到?

回答by harm

It is unlikely that you dohave a session and don'thave a referrer.

这是不可能的,你这样做有一个会议,并没有有引用。

The situation that a referrer is not set isn't that uncommon and I usually rescue that expection:

未设置推荐人的情况并不少见,我通常会挽救这种期望:

def some_method
  redirect_to :back
rescue ActionController::RedirectBackError
  redirect_to root_path
end

If you do this often (which I think is a bad idea) you can wrap it in an other method like Maran suggests.

如果您经常这样做(我认为这是一个坏主意),您可以将其包装在 Maran 建议的其他方法中。

BTW I think that's a bad idea because this makes the userflow ambiguous. Only in the case of a login this is sensible.

顺便说一句,我认为这是一个坏主意,因为这会使用户流程模棱两可。只有在登录的情况下,这是明智的。

UPDATE: As several people pointed out this no longer workswith Rails 5. Instead, use redirect_back, this method also supports a fallback. The code then becomes:

更新:正如一些人指出的,这不再适用于 Rails 5。相反,使用redirect_back,此方法还支持回退。然后代码变成:

def some_method
  redirect_back fallback_location: root_path
end

回答by Mirko

Here's my little redirect_to_back method:

这是我的小 redirect_to_back 方法:

  def redirect_to_back(default = root_url)
    if request.env["HTTP_REFERER"].present? and request.env["HTTP_REFERER"] != request.env["REQUEST_URI"]
      redirect_to :back
    else
      redirect_to default
    end
  end

You can pass an optional url to go somewhere else if http_refferrer is blank.

如果 http_referrer 为空,您可以传递一个可选的 url 到其他地方。

回答by Maran

def store_location
  session[:return_to] = request.request_uri
end

def redirect_back_or_default(default)
  redirect_to(session[:return_to] || default)
  session[:return_to] = nil
end

Try that! (Thanks to the Authlogicplugin)

试试吧!(感谢Authlogic插件)

回答by Cyril Duchon-Doris

Core feature

核心功能

redirect_backis a core feature from Rails 5+, it is available in the ActionController::Redirectingmodule that is already included inApplicationController::Base.

redirect_back是 Rails 5+ 的核心特性,它在ActionController::Redirecting已经包含在ApplicationController::Base.

DEPRECATION WARNING: redirect_to :backis deprecated and will be removed from Rails 5.1. Please use redirect_back(fallback_location: fallback_location)where fallback_locationrepresents the location to use if the request has no HTTP referer information.

弃用警告:redirect_to :back已弃用,将从 Rails 5.1 中删除。如果请求没有 HTTP 引用信息,请使用redirect_back(fallback_location: fallback_location)wherefallback_location表示要使用的位置。

EDIT : source

编辑:来源

回答by troex

Maybe it's late but I would like to share my method which also preserve options:

也许已经晚了,但我想分享我的方法,它也保留了选项:

  def redirect_back_or_default(default = root_path, *options)
    tag_options = {}
    options.first.each { |k,v| tag_options[k] = v } unless options.empty?
    redirect_to (request.referer.present? ? :back : default), tag_options
  end

You can use it like:

你可以像这样使用它:

redirect_back_or_default(some_path, :notice => 'Hello from redirect', :status => 301)

回答by longkt90

similar to @troex's answer, add this to your application controller

类似于@troex 的回答,将此添加到您的应用程序控制器

def redirect_back_or_default(default = root_path, options = {})
  redirect_to (request.referer.present? ? :back : default), options
end

then use it in your controller

然后在您的控制器中使用它

redirect_back_or_default answer_path(answer), flash: { error: I18n.t('m.errors')}

回答by Rahul Goyal

Recently, I encountered the same issue where either I had to redirect :backor to specific page. After going to a lot of solution, I finally found this which is simple and seems to solve the issue:

最近,我遇到了同样的问题,我必须重定向:back或重定向到特定页面。经过大量解决方案后,我终于发现这很简单,似乎解决了问题:


if request.env["HTTP_REFERER"].present?
    redirect_to :back
else
    redirect_to 'specific/page'
end

If you want to use sessiondetails, do it in the else part of the code.

如果要使用session详细信息,请在代码的 else 部分进行。