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:

  #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:

     #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:

#Redirect user to previous page
      go_back

      #Return to prevent multiple redirects
      return

 

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

 

Posted

in

,

by

Comments

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create a website or blog at WordPress.com

%d bloggers like this: