# How to return to last viewed page?

**URL:** <https://www.ruby-forum.com/t/how-to-return-to-last-viewed-page/108263>\
**Category:** Rails\
**Created:** [July 1, 2007, 8:22am UTC](https://www.ruby-forum.com/t/how-to-return-to-last-viewed-page/108263 "2007-07-01T08:22:51Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![lunaclaire](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f4b2a3/32.png) [@lunaclaire](https://www.ruby-forum.com/u/lunaclaire)\
**Post date:** [July 1, 2007, 8:22am UTC](https://www.ruby-forum.com/t/how-to-return-to-last-viewed-page/108263/1 "2007-07-01T08:22:51Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![lunaclaire](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f4b2a3/32.png) [@lunaclaire](https://www.ruby-forum.com/u/lunaclaire)\
**Post date:** [July 1, 2007, 8:51am UTC](https://www.ruby-forum.com/t/how-to-return-to-last-viewed-page/108263/2 "2007-07-01T08:51:19Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![lunaclaire](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f4b2a3/32.png) [@lunaclaire](https://www.ruby-forum.com/u/lunaclaire)\
**Post date:** [July 1, 2007, 8:47pm UTC](https://www.ruby-forum.com/t/how-to-return-to-last-viewed-page/108263/3 "2007-07-01T20:47:03Z")

</div>

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. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![lunaclaire](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f4b2a3/32.png) [@lunaclaire](https://www.ruby-forum.com/u/lunaclaire)\
**Post date:** [July 1, 2007, 10:04pm UTC](https://www.ruby-forum.com/t/how-to-return-to-last-viewed-page/108263/4 "2007-07-01T22:04:11Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![lunaclaire](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f4b2a3/32.png) [@lunaclaire](https://www.ruby-forum.com/u/lunaclaire)\
**Post date:** [July 2, 2007, 12:15am UTC](https://www.ruby-forum.com/t/how-to-return-to-last-viewed-page/108263/5 "2007-07-02T00:15:25Z")

</div>

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/](http://www.bencurtis.com/) – blog  
[http://agilewebdevelopment.com/rails-ecommerce](http://agilewebdevelopment.com/rails-ecommerce) – build e-commerce  
sites with Rails
