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.   #Redirect_to_back
  2.   def go_back
  3.  
  4.     #Attempt to redirect
  5.     redirect_to :back
  6.  
  7.     #Catch exception and redirect to root
  8.     rescue ActionController::RedirectBackError
  9.       redirect_to root_path
  10.    end

 

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

  1.      #Redirect user to previous page
  2.       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. #Redirect user to previous page
  2.       go_back
  3.  
  4.       #Return to prevent multiple redirects
  5.       return

 

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

 

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.