Coding is like gardening...

redirect_to :back not reliable

Suppose you want to allow someone to make a comment and then return to the page they were on. An easy way to do this in Rails is to set the CommentsController method to redirect_to :back

def create
  comment = Comment.new(params[:comment])
  if comment.save
    redirect_to :back
  end
end

But beware! The trouble with this is that Firefox has an option to disable referrers, so you can’t rely upon the HTTP_REFERER being set.

So you could set up a session variable when you display your comment form, to later be used in the controller. The current url is found in request.path

<% session[:return_to] = request.path -%>
def create
  comment = Comment.new(params[:comment])
  if comment.save
    redirect_to session[:return_to]
  end
end

Sure, people can turn off session cookies, but if they do that then you have all sorts of other Rails problems to do with authenticity tokens and user authentication, so I think this is a fairly safe method.

Of course, an even better option is to add the comment by AJAX and avoid the need for a page refresh at all … but that can be the topic of a different blog post!

15 Responses to “redirect_to :back not reliable”

  1. Different point of view from that post. Interesting to say the least.

  2. Interesting post. I have made a twitter post about this. Others no doubt will like it like I did.

  3. Ryand77skyers says:

    Sounds like some great advice. I would think that terrified is not 100% the best word to use in “Be terrified every day”, I think excited could also work there. Everything in the space is constantly changing and evolving, so hrsaccount designers have a new opportunity to potentially come up with something revolutionary every day. And it is kind of funny to read “Embrace Arial” written in Times New Roman font.

  4. Great posting. I like this.
    Thanks for sharing your tips.And it is kind of funny to read “Embrace Arial” written in Times New Roman font.

  5. how to understand this code

  6. I wonder if it is in the works for this type of thing to be programmed into disqus from the get-go?

  7. bible study says:

    nice post, thanks

  8. richardz315 says:

    wow i really found this to be interesting. thanks for sharing

  9. This a spot on analogy made me chuckle.

  10. mtravel13 says:

    exactly ! now a days many webmasters use ajax to avoid the page and give user a consistent feeling , and by the way many dont use firefox so for them HTTP_REFERER method will do

  11. Classical says:

    Thanks for sharing that interesting codes. That's really helpful.

  12. Great blog.It is really informative article.The idea you sharing about CommentsController method gave me some food for thoughts.I am thinking of trying it now!Hope i will manage!Thank you for sharing your experience and teaching us.

  13. kentuckyrealestate says:

    Inventory Control
    Wow a great posting. I like this. Thanks for sharing yours tips…..

  14. So you could set up a session variable when you display your comment form, to later be used in the controller. The current url is found in request.path

    Thanks and Regards

  15. Puppies4sale says:

    Thanks for sharing such an important tips with us. Great Article. looking for more tips.