No HTTP_REFERER was set in the request to this action, so redirect_to :back could not be called successfully

Well, I’ve managed to break my app again today – getting the following error message:

ActionController::RedirectBackError in OrdersController#new

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”].

 

Thankfully this is another one that’s fairly easy to fix. Simply create the following method – I’ve added mine to the application controller:

1
2
3
4
5
6
7
8
9
10
  #Redirect_to_back
  def go_back
 
    #Attempt to redirect
    redirect_to :back
 
    #Catch exception and redirect to root
    rescue ActionController::RedirectBackError
      redirect_to root_path
   end

 

You can then call this method from any of you controllers using the following code:

1
2
     #Redirect user to previous page
      go_back

 

You may get the following error depending on how the rest of your controller action is setup:

Render and/or redirect were called multiple times in this action. Please note that you may only call render OR redirect, and at most once per action. Also note that neither redirect nor render terminate execution of the action, so if you want to exit an action after redirecting, you need to do something like “redirect_to(…) and return”.

 

This message is pretty self explanatory – simply add return immediately after your call to go_back in order to prevent any additional redirects being called:

1
2
3
4
5
#Redirect user to previous page
      go_back
 
      #Return to prevent multiple redirects
      return

 

Hopefully that’ll help someone out there, Good Luck!

 

Leave a Reply