# Storing URL locations

**URL:** <https://www.ruby-forum.com/t/storing-url-locations/154239>\
**Category:** Rails\
**Created:** [November 26, 2008, 5:32pm UTC](https://www.ruby-forum.com/t/storing-url-locations/154239 "2008-11-26T17:32:50Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![elioncho](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ed8c4c/32.png) [@elioncho](https://www.ruby-forum.com/u/elioncho)\
**Post date:** [November 26, 2008, 5:32pm UTC](https://www.ruby-forum.com/t/storing-url-locations/154239/1 "2008-11-26T17:32:50Z")

</div>

I have a friendship system where users can create new friendships from  
different views. The idea is to redirect them to where they initially  
requested, accepted, etc… the friendship. To do this, I need to  
store the URL path from where they are sending the request. Is there a  
way to save the initial URL (from where the request mas made to the  
friendships controller). Any light on this?

Thanks for your time,

Elías

---

<div class="post-metadata">

**Author:** ![elioncho](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ed8c4c/32.png) [@elioncho](https://www.ruby-forum.com/u/elioncho)\
**Post date:** [November 26, 2008, 5:36pm UTC](https://www.ruby-forum.com/t/storing-url-locations/154239/2 "2008-11-26T17:36:58Z")

</div>

I use the following code to redirect them to the page they requested  
before  
they logged in. Maybe this will get you on the right path. I put it in  
the  
controller under protected.

# Protect a page from unauthorized access.

def protect  
unless logged\_in?  
session[:protected\_page] = request.request\_uri  
flash[:notice] = “Please log in first”  
redirect\_to :action =\> “login”  
return false  
end  
end

# Redirect to the previously requested URL (if present).

def redirect\_to\_forwarding\_url  
if (redirect\_url = session[:protected\_page])  
session[:protected\_page] = nil  
redirect\_to redirect\_url  
else  
redirect\_to :action =\> “index”  
end  
end

Sean McGilvray & Sarena Byers  
Executive Director  
Identity Theft Specialist  
Pre-Paid Legal Service’s, Inc. NYSE:PPD  
Phone: 760-486-1019  
removed\_email\_address@domain.invalid  
[http://www.transferhome.net](http://www.transferhome.net)

---

<div class="post-metadata">

**Author:** ![elioncho](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ed8c4c/32.png) [@elioncho](https://www.ruby-forum.com/u/elioncho)\
**Post date:** [November 26, 2008, 5:54pm UTC](https://www.ruby-forum.com/t/storing-url-locations/154239/3 "2008-11-26T17:54:41Z")

</div>

Hi Sean thanks for the response.

I thought about a solution based on your answer, but I am not happy  
with it. My solution consisted on having a before\_filter on the  
controllers I know there might be a call to the friendships  
controller. This before\_filter will be used to save the URL. And then,  
have a before\_filter on the application controller where I do this:

if params[:controller] != “friendships”  
session[:return\_to] = nil  
end

The thing is that having this before\_filter every time for every  
controller doesn’t seems right.

Thanks,

Elías
