How to return to last viewed page?

I have a Contact Us link in the footer of most of my pages. Clicking
this takes the user to a new page containing a form for sending us a
msg of some sort.

The form has Submit and Cancel buttons.

After the user clicks one of these buttons to post the message, I’d
like to return them to whatever page they were viewing before they
clicked Contact Us.

Anyway to do this?

lunaclaire wrote:

I have a Contact Us link in the footer of most of my pages. Clicking
this takes the user to a new page containing a form for sending us a
msg of some sort.
Anyway to do this?

@request.env[“HTTP_REFERER”]

thing is, if the user got to the form from say, google, the http_referer
would redirect back to the google search result page…a different
option is to store the id of the page that was redirected to in the
params, and redirect back to there when the post is
submitted…params[:redirect] or something like that.

whichever u choose.

Thanks, Shai.

I like the second option since it seems more robust… but I’m missing
what you mean by “store the id of the page”… what id would serve as
the identifier in this case?

On Jun 30, 11:51 pm, Shai R. [email protected]

BTW -

I tried using

redirect :back

which seems obvious and I think is quite similar to what Shai is
suggesting, but all it does is return to the Contact Us form page

any thoughts?

I usually do this by appending a :return_to => request.request_uri at
the end of the link_to that links to the contact form. Then add a
before filter that stuffs that parameter into the session, and use
that session variable for the destination for the redirect after the
form has been submitted. If you are using acts_as_authenticated, you
already have the redirect_back_or_default method that makes this
easy. The cancel button can also take a peek at this session
variable to know what its destination should be.


Benjamin C.
http://www.bencurtis.com/ – blog
http://agilewebdevelopment.com/rails-ecommerce – build e-commerce
sites with Rails