Correctly doing redirect_to :back in Ruby on Rails

2019-01-12 19:20发布

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

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\"]."

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?

7条回答
姐就是有狂的资本
2楼-- · 2019-01-12 19:26

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


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

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

查看更多
Animai°情兽
3楼-- · 2019-01-12 19:28

It is unlikely that you do have a session and don't have 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.

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 works with Rails 5. Instead, use redirect_back, this method also supports a fallback. The code then becomes:

def some_method
  redirect_back fallback_location: root_path
end
查看更多
ら.Afraid
4楼-- · 2019-01-12 19:40
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 Authlogic plugin)

查看更多
做自己的国王
5楼-- · 2019-01-12 19:42

Here's my little redirect_to_back method:

  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.

查看更多
我想做一个坏孩纸
6楼-- · 2019-01-12 19:47

Core feature

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

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

EDIT : source

查看更多
我命由我不由天
7楼-- · 2019-01-12 19:48

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

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')}
查看更多
登录 后发表回答