# Somebody help me to get previous page url in RoR

**URL:** <https://www.ruby-forum.com/t/somebody-help-me-to-get-previous-page-url-in-ror/133768>\
**Category:** Rails\
**Created:** [April 3, 2008, 11:37am UTC](https://www.ruby-forum.com/t/somebody-help-me-to-get-previous-page-url-in-ror/133768 "2008-04-03T11:37:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![karthikragu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/a88e57/32.png) [@karthikragu](https://www.ruby-forum.com/u/karthikragu)\
**Post date:** [April 3, 2008, 11:37am UTC](https://www.ruby-forum.com/t/somebody-help-me-to-get-previous-page-url-in-ror/133768/1 "2008-04-03T11:37:51Z")

</div>

hi all,  
im having a view page called “edit” and im having a back button  
in it.im using this edit view page in many place of my application.i  
want to go back to the page which calls this “edit” page no matter what  
that page is.so somebody please help me to get the previous page url in  
ruby on rails.waiting for the response.

regards,

karthik.

---

<div class="post-metadata">

**Author:** ![karthikragu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/a88e57/32.png) [@karthikragu](https://www.ruby-forum.com/u/karthikragu)\
**Post date:** [April 3, 2008, 12:00pm UTC](https://www.ruby-forum.com/t/somebody-help-me-to-get-previous-page-url-in-ror/133768/2 "2008-04-03T12:00:41Z")

</div>

request.referer might be what you want ^-^

On Apr 3, 5:37 pm, Karthikragunath B. \<rails-mailing-l…@andreas-

---

<div class="post-metadata">

**Author:** ![karthikragu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/a88e57/32.png) [@karthikragu](https://www.ruby-forum.com/u/karthikragu)\
**Post date:** [April 3, 2008, 12:13pm UTC](https://www.ruby-forum.com/t/somebody-help-me-to-get-previous-page-url-in-ror/133768/3 "2008-04-03T12:13:30Z")

</div>

Karthikragunath B. wrote:

> hi all,  
> im having a view page called “edit” and im having a back button  
> in it.im using this edit view page in many place of my application.i  
> want to go back to the page which calls this “edit” page no matter what  
> that page is.so somebody please help me to get the previous page url in  
> ruby on rails.waiting for the response.
> 
> regards,
> 
> karthik.

How i normally do this is to have an application.rb method which saves  
the current url to the session, and another that redirects to the stored  
url (which should have a default in case it can’t find it). Then, in  
pages which i want to bookmark for returning to (you might not want  
someone to get sent back to some pages), then save the url.

EG in application.rb

def store\_location  
session[‘saved\_location’] = request.request\_uri  
end

def redirect\_to\_back\_or\_default(default)  
if session[‘saved\_location’].nil?  
redirect\_to default  
else  
redirect\_to session[‘return-to’]  
session[‘saved\_location’] = nil  
end  
end

Then, in a page that you want the user to be able to go back to:

\<% store\_location %\>

Then, just call  
'redirect\_to\_back\_or\_default(‘enter your chosen default url here,  
depending on context’)

to send someone back.

I think i copied this off someone else a while ago so props if this is  
your code 🙂

---

<div class="post-metadata">

**Author:** ![karthikragu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/a88e57/32.png) [@karthikragu](https://www.ruby-forum.com/u/karthikragu)\
**Post date:** [April 3, 2008, 1:12pm UTC](https://www.ruby-forum.com/t/somebody-help-me-to-get-previous-page-url-in-ror/133768/4 "2008-04-03T13:12:28Z")

</div>

Thanks williams,

```
              its working great.thanks for ur timely help.
```
