ydzvaa
Last Updated: September 13, 2016
·
7.444K
· mrako
4fe949ea9ee7c0bc5eb4bf3e17b09d75

Redirect to back or default in Rails

What happens if your user user cannot access a resource in your application? Do you redirect the user back to the previous page?

redirect_to :back

Unfortunately :back depends on the request header variable HTTP_REFERER, and thus you might run into trouble if it does not exist. This might be the case if your user is for example trying to access the resource with a direct link (e.g. a bookmark or a link from an email).

Instead you should extract the back-functionality, and create a common method for redirection with an extra fallback:

def redirect_to_back_or_default(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

And then use it in your controllers instead of the redirect_to :back like so:

def show
  ...
  redirect_to_back_or_default
  ...
end

Now your user is redirected to the root_url (changeable with the default-parameter) if HTTP_REFERER does not exist.

You should also remember the other formats and give an error message instead of the redirection:

respond_to do |format|
  format.html { redirect_to_back_or_default }
  format.json { render json:
    {errors: [I18n.t("some.error.message")]}, status: 404
  }
end

See redirect_to in Rails API documentation

Say Thanks
Respond

3 Responses
Add your response

16856
E0a4126e958fdd08425b773421732aad

Wanted to make a small amendment. Add *args so that you can pass through other options like notices.

def redirect_to_back_or_default(default = root_url, *args)
  if request.env['HTTP_REFERER'].present? && request.env['HTTP_REFERER'] != request.env['REQUEST_URI']
    redirect_to :back, *args
  else
    redirect_to default, *args
  end
end
over 1 year ago ·
26458
None

why do you have to check 'request.env["HTTPREFERER"] != request.env["REQUESTURI"]'? Don't know when they would be equal.

thanks

over 1 year ago ·
28154

This check 'request.env["HTTPREFERER"] != request.env["REQUESTURI"]' is required to avoid multiple calls to the same url.

over 1 year ago ·