views:

17

answers:

2

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.

Any ideas?

A: 

Try redirect_to(:back).

Andrew Vit
A: 

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).

Daniel Vandersluis
Thanks! This works nicely if someone fills out the form correctly.However, do you have any ideas of how to re-render a form partial to a different controller's action if validation fails? Thanks again.
TMB
@TMB: As that's a separate issue to what this question is talking about, you should ask that as a separate question.
Daniel Vandersluis