Correctly Doing Redirect_To :Back in Ruby on Rails When Referrer Is Not Available

Rails error: No HTTP_REFERER was set in the request to this action

what is HTTP_REFERER?

The HTTP referer is an HTTP header field that identifies the address of the webpage (i.e. the URI or IRI) that linked to the resource being requested. This is set by ActionController::Request object.

why redirect to back will trigger this error?

This usually happens when request.env["HTTP_REFERER"] is not set. (I also wonder to know in which case it is set and not set)

You could refer this answer to fix your issue.

(OR) I would highly prefer to define a custom page for access denied and redirect to it instead of redirecting :back (which I think bad idea)

For example from cancan gem docs,

rescue_from CanCan::AccessDenied do |exception|
render :file => "#{Rails.root}/public/403.html", :status => 403, :layout => false
## to avoid deprecation warnings with Rails 3.2.x (and incidentally using Ruby 1.9.3 hash syntax)
## this render call should be:
# render file: "#{Rails.root}/public/403", formats: [:html], status: 403, layout: false
end

Hope this helps!

How can I go back to the previous page after updating?

You can't call request.referer to directly back the the previous of edit page. Cause on the update method, the request.referer will be always the edit path.

You can, do a trick like, parse the request.referer of edit action (might be the index one, for ex. as params to update action, and redirect to the path on updating the resource )

# edit form view
<%= hidden_field_tag :previous_request, request.referer %>

# update action
def update
...
redirect_to params[:previous_request]
end

Redirecting from an update action to the referrer of the edit

You can memorize the url in the session. This is how I implemented this feature in my application.

Add these methods to your ApplicationController

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

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

def http_referrer
http_referer, request_path = request.env["HTTP_REFERER"],
request.env["REQUEST_PATH"]
return nil unless (http_referer and request_path and
http_referer =~ Regexp.new("^#{SITE_ROOT}") and
http_referer != (SITE_ROOT + request_path))
return http_referer
end

In UserController edit/new action store the referrer.

before_filter :store_location, :only => [:new, :edit]

def store_location
super http_referrer
end

In UserController create/update action return and reset referrer.

def create
if @user.save
flash[:notice] = "Successfully created the user."
redirect_back_or_default root_url
else
render :action => 'new'
end
end

def update
if @user.save
flash[:notice] = "Successfully created the user."
redirect_back_or_default root_url
else
render :action => 'new'
end
end

rails redirect_to :back not working

Rails 5 has redirect_back, instead of redirect_to :back. It was changed as it used to raise an exception when request's HTTP_REFERER was not present.

So use this:

redirect_back fallback_location: root_path

You can change root_path to something else as per your requirements.

How to redirect to previous page in Ruby On Rails?

In your edit action, store the requesting url in the session hash, which is available across multiple requests:

session[:return_to] ||= request.referer

Then redirect to it in your update action, after a successful save:

redirect_to session.delete(:return_to)

Preserving referrer on redirect_to in Ruby on Rails

There are a handful of ways detailed in this question.

My preferred way to do it is set a before_filter for store_referrer in your controller that calls a method like this in my application_controller. Then use the redirect_back_or_default method in your actions to handle the re-direct.

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

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

You can also look at the redirect_to :back method that is available in Rails, or if you're using Authlogic/Devise, they both come with similar helpers to do the same thing.

Take a look through the linked thread, this suggestion by troex is a good one.

rails - redirect back unless previous page was artist page

Perhaps request.referer is what you're looking for? Then you can say something like:

if request.referer == artist_url(track.artist)
redirect_to [track.artist, track]
else
redirect_to :back
end


Related Topics



Leave a reply



Submit