Rails 2.3 - Redirection Issues
I have an application where folks search for an item. If the item is found, they are presented with a list of related items found by parameters in the URL AND a contact form. I'm having issues redirecting the visitor back to the same page (with the URL parameters) after submitting the form.
开发者_如何学CAny ideas?
Try redirect_to(:back)
.
You could use redirect_to :back
, but note that this depends on the Referer
header being set, and you'll run into an error if it's not for some reason.
To get around this, I use a method like the following in my application (I put it into ApplicationController
so it's accessible in all my controllers):
def redirect_back_or_to(options = {})
if request.env["HTTP_REFERER"].blank?
redirect_to options
else
redirect_to :back
end
end
Which will redirect back if the Referer
header is set and otherwise work like a normal redirect_to (so you can specify where to redirect to by default).
精彩评论