# Best Way To Get Where You Have Been

**URL:** <https://www.ruby-forum.com/t/best-way-to-get-where-you-have-been/48366>\
**Category:** Rails\
**Created:** [November 13, 2005, 11:17pm UTC](https://www.ruby-forum.com/t/best-way-to-get-where-you-have-been/48366 "2005-11-13T23:17:30Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![john\_old](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9f8e36/32.png) [@john\_old](https://www.ruby-forum.com/u/john_old)\
**Post date:** [November 13, 2005, 11:17pm UTC](https://www.ruby-forum.com/t/best-way-to-get-where-you-have-been/48366/1 "2005-11-13T23:17:30Z")

</div>

Maybe not the most descriptive subject! 🙂

I have a filter that redirects the user to a login page if they have  
not logged in on certain pages. Now what is the best way to get the  
page they where initially requesting and then send the user to that  
page after they have successfully logged in?

John K.

–  
[http://www.soen.info](http://www.soen.info) - where software engineering knowledge gets indexed  
[http://cusec.soen.info](http://cusec.soen.info) - software engineering conference

---

<div class="post-metadata">

**Author:** ![john\_old](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9f8e36/32.png) [@john\_old](https://www.ruby-forum.com/u/john_old)\
**Post date:** [November 13, 2005, 11:53pm UTC](https://www.ruby-forum.com/t/best-way-to-get-where-you-have-been/48366/2 "2005-11-13T23:53:32Z")

</div>

You can do:

redirect\_to :back

From the manual:

“🔙 Back to the page that issued the request. Useful for forms  
that are triggered from multiple places. Short-hand for redirect\_to  
(request.env[“HTTP\_REFERER”])”

Rob

---

<div class="post-metadata">

**Author:** ![john\_old](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9f8e36/32.png) [@john\_old](https://www.ruby-forum.com/u/john_old)\
**Post date:** [November 14, 2005, 12:08am UTC](https://www.ruby-forum.com/t/best-way-to-get-where-you-have-been/48366/3 "2005-11-14T00:08:33Z")

</div>

PS: However, that only works if you have the login form embedded in  
your pages. If you have an link that takes the user to a intermediary  
login form, then the solution I proposed doesn’t work. There’s  
probably another way to do it then, but I don’t know about it.

Rob

---

<div class="post-metadata">

**Author:** ![john\_old](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9f8e36/32.png) [@john\_old](https://www.ruby-forum.com/u/john_old)\
**Post date:** [November 14, 2005, 12:20am UTC](https://www.ruby-forum.com/t/best-way-to-get-where-you-have-been/48366/4 "2005-11-14T00:20:34Z")

</div>

Save it in the session before you redirect…

session[:where\_you\_wanted\_to\_go] = request.parameters

# Then redirect to login

redirect\_to (:controller =\> “login”, :action =\> “login”)

The code below goes in your login controller and executes once login is  
successful:

where\_you\_wanted\_to\_go = session[:where\_you\_wanted\_to\_go] || {:action =\>  
“default\_page”}  
session[:where\_you\_wanted\_to\_go] = nil  
redirect\_to(where\_you\_wanted\_to\_go)

I may not have this 100%, I’m pulling it from memory, I believe I saw it  
covered in the Agile Rails Development book…

Hope this helps,

–Ryan

----- Original Message -----  
From: “John K.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
To: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Sent: Sunday, November 13, 2005 5:14 PM  
Subject: [Rails] Best Way To Get Where You Have Been

---

<div class="post-metadata">

**Author:** ![john\_old](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9f8e36/32.png) [@john\_old](https://www.ruby-forum.com/u/john_old)\
**Post date:** [November 14, 2005, 1:20am UTC](https://www.ruby-forum.com/t/best-way-to-get-where-you-have-been/48366/5 "2005-11-14T01:20:39Z")

</div>

Most off the login systems spawned from the original login generator  
include two methods which try to handle this: store\_location and  
redirect\_back\_or\_default - you can look at the login generator, salted  
hash login generate, login engine and acts\_as\_authenticated  
generator/plugin for examples of these methods to copy, and how to use  
them in your own application.

- james

---

<div class="post-metadata">

**Author:** ![john\_old](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9f8e36/32.png) [@john\_old](https://www.ruby-forum.com/u/john_old)\
**Post date:** [November 14, 2005, 1:41am UTC](https://www.ruby-forum.com/t/best-way-to-get-where-you-have-been/48366/6 "2005-11-14T01:41:41Z")

</div>

Here is a link to a blog that helped me solve a similar problem:

> **[BigBold - Informasi Tentang Bisnis dan Marketing](http://www.bigbold.com)**
>
> BigBold ialah situs yang memberikan segala macam Informasi Tentang Bisnis dan Marketing

I took the code from the site and added two minor changes to handle an  
empty session and new/create duplication and it works beautifully for  
me. Just add the redirect\_back to your actions.

Regards,

Michael

Application Controller:

# Store where we are

def store\_locations

if @session[‘prevpage’] && @session[‘thispage’] != @request.request\_uri

#If this is a “create” action, the do not do anything, because it will  
just take us to the new

unless @request.request\_uri.index(‘create’)

@session[‘prevpage’] = @session[‘thispage’] || ‘’

@session[‘thispage’] = @request.request\_uri

end

else

@session[‘prevpage’] = @request.request\_uri

@session[‘thispage’] = @request.request\_uri

end

end

…

# redirect\_back

# If a previous page is stored in the session, go back to it… otherwise

go back to a default page

def redirect\_back(default)

if @session[‘prevpage’].nil?

if default

redirect\_to default

else

redirect\_to :controller =\> “”, :action =\> “”

end

else

if @session[‘prevpage’].length \> 4

redirect\_to\_url @session[‘prevpage’]

else

redirect\_to default

end

end

end

removed\_email\_address@domain.invalid wrote:  
Save it in the session before you redirect…

session[:where\_you\_wanted\_to\_go] = request.parameters

# Then redirect to login

redirect\_to (:controller =\> “login”, :action =\> “login”)

The code below goes in your login controller and executes once login is  
successful:

where\_you\_wanted\_to\_go = session[:where\_you\_wanted\_to\_go] || {:action =\>  
“default\_page”}  
session[:where\_you\_wanted\_to\_go] = nil  
redirect\_to(where\_you\_wanted\_to\_go)

I may not have this 100%, I’m pulling it from memory, I believe I saw it  
covered in the Agile Rails Development book…

Hope this helps,

–Ryan

----- Original Message -----  
From: “John K.”  
To:  
Sent: Sunday, November 13, 2005 5:14 PM  
Subject: [Rails] Best Way To Get Where You Have Been

---

<div class="post-metadata">

**Author:** ![john\_old](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9f8e36/32.png) [@john\_old](https://www.ruby-forum.com/u/john_old)\
**Post date:** [November 14, 2005, 1:44am UTC](https://www.ruby-forum.com/t/best-way-to-get-where-you-have-been/48366/7 "2005-11-14T01:44:42Z")

</div>

-----BEGIN PGP SIGNED MESSAGE-----  
Hash: SHA1

On Nov 13, 2005, at 2:52 PM, Robert wrote:

> You can do:
> 
> redirect\_to :back
> 
> From the manual:
> 
> “🔙 Back to the page that issued the request. Useful for forms  
> that are triggered from multiple places. Short-hand for redirect\_to  
> (request.env[“HTTP\_REFERER”])”

This won’t work:

- redirect to /login
- post form to /login
- redirect :back sends you to the REFERER, which is /login

You can use something like:  
def redirect\_back(fallback\_options)  
redirect\_to params[:return\_to] || session[:return\_to] ||  
fallback\_options  
end  
and pass :return\_to as a hidden form parameter or set session  
[:return\_to] = request.request\_uri before you redirect to login.

jeremy  
-----BEGIN PGP SIGNATURE-----  
Version: GnuPG v1.4.2 (Darwin)

iD8DBQFDd93tAQHALep9HFYRAj0oAJ9RRn5NWnCzX78HyfizjsMpbFDEXwCgkzZ9  
luDOitdtG2oMjonc1XwH0Jo=  
=RAoG  
-----END PGP SIGNATURE-----
