Tag Archives: redirect_to

Redirect to Root – C# MVC4

Hey everyone,

Just a quick one, how to redirect to root from a controller:

return Redirect(Url.Content("~/"));

For more info, checkout this Stackoverflow post: http://stackoverflow.com/a/10292039/522859

Flash Notice Appearing Twice – Ruby on Rails

Ran into another flash notice issue, my messages were not disappearing after the first view. My code was as follows:

#Create flash warning
flash[:warning] = ("This product has been replaced: <a href='#{url_for(new_product)}'>" +new_product.title + "</a>").html_safe

It turns out that all you have to do to fix this issue is the following:

flash.now[:warning] = ("This product has been replaced: <a href='#{url_for(new_product)}'>" +new_product.title + "</a>").html_safe

This blog gives a brief overview of when to use flash.now[notice] and when to use flash[:notice]. A basic rule of thumb is that if you’re using a redirect, use flash[:notice], if you’re simply rendering a view, use flash.now[:notice].

Error 324 (net::ERR_EMPTY_RESPONSE) – Ruby on Rails

I managed to break my app again while playing around with the routes and a few redirects:

No data received
Unable to load the webpage because the server sent no data.
Here are some suggestions:
Reload this webpage later.
Error 324 (net::ERR_EMPTY_RESPONSE): The server closed the connection without sending any data.

 

This wasn’t one I had encountered before and unfortunately Google couldn’t give me anything to cheat off. Luckily, a quick review of the server log revealed the following message just before it closed the connection:

[2011-12-03 21:09:05] ERROR URI::InvalidURIError: the scheme http does not accept registry part: 192.168.1.3:3000locations (or bad hostname?)

 

From this error message it became clear that I needed to place a forward slash before my redirect path:

1
2
#Redirect to edit location path      
redirect_to '/locations/edit'

 

Hopefully this’ll be able to help someone else out there, Good Luck!

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!