# "Remembering" link to redirect to after logging in

**URL:** <https://www.ruby-forum.com/t/remembering-link-to-redirect-to-after-logging-in/84608>\
**Category:** Rails\
**Created:** [March 21, 2006, 11:36am UTC](https://www.ruby-forum.com/t/remembering-link-to-redirect-to-after-logging-in/84608 "2006-03-21T11:36:01Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![generik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/4da419/32.png) [@generik](https://www.ruby-forum.com/u/generik)\
**Post date:** [March 21, 2006, 11:36am UTC](https://www.ruby-forum.com/t/remembering-link-to-redirect-to-after-logging-in/84608/1 "2006-03-21T11:36:01Z")

</div>

Hi,

Suppose I have a resource such as [http://localhost:3000/topsecret/data](http://localhost:3000/topsecret/data)  
that requires the user to login first.

What I did was make use of before\_filter to check and see if the session  
variable is set with the logged in user’s id (similar to the example in  
“Agile Web D. with Rails”).

However while the filtering function does work correctly in redirecting  
the user to the login page, how do I actually capture the original  
request made by the user so that I can redirect the user back to the  
required resource after s/he has logged in?

Thanks!

---

<div class="post-metadata">

**Author:** ![generik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/4da419/32.png) [@generik](https://www.ruby-forum.com/u/generik)\
**Post date:** [March 21, 2006, 11:50am UTC](https://www.ruby-forum.com/t/remembering-link-to-redirect-to-after-logging-in/84608/2 "2006-03-21T11:50:20Z")

</div>

For doing this I usually use a session[]. The user tries to login, and  
if  
it’s not authenticated the authentication method stores the  
“intended\_controller” in a session[]. Then when the user logs in the  
system the login method redirects to the previously intended controller.

Hope that helps,

Francesc

* * *

##########################################

# The Admin controller

##########################################

before\_filter :authentication

def authentication  
unless session[:user]  
session[:intended\_controller] = ‘/admin/’ + controller\_name  
redirect\_to :controller =\> ‘/account’  
end  
end

#########################################

# The account controller

#########################################

def login

# Here comes the login code …

if # login is successful …  
redirect\_to session[:current\_page]  
else  
redirect\_to ‘/account/login’  
end

* * *

–  
name. Francesc E.  
email. removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![generik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/4da419/32.png) [@generik](https://www.ruby-forum.com/u/generik)\
**Post date:** [March 21, 2006, 12:02pm UTC](https://www.ruby-forum.com/t/remembering-link-to-redirect-to-after-logging-in/84608/3 "2006-03-21T12:02:29Z")

</div>

On Mar 21, 2006, at 11:36, Woei S. wrote:

> redirecting  
> the user to the login page, how do I actually capture the original  
> request made by the user so that I can redirect the user back to the  
> required resource after s/he has logged in?

acts\_as\_authenticated[\*] does this out of the box. However, if you  
want to write your own system take a look at the method  
redirect\_back\_or\_default here

```
 http://tinyurl.com/m62y5

```

– fxn

[\*] [http://wiki.rubyonrails.org/rails/pages/Acts\_as\_authenticated](http://wiki.rubyonrails.org/rails/pages/Acts_as_authenticated)

---

<div class="post-metadata">

**Author:** ![generik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/4da419/32.png) [@generik](https://www.ruby-forum.com/u/generik)\
**Post date:** [March 21, 2006, 12:19pm UTC](https://www.ruby-forum.com/t/remembering-link-to-redirect-to-after-logging-in/84608/4 "2006-03-21T12:19:56Z")

</div>

Xavier N. wrote:

> On Mar 21, 2006, at 11:36, Woei S. wrote:
> 
> > redirecting  
> > the user to the login page, how do I actually capture the original  
> > request made by the user so that I can redirect the user back to the  
> > required resource after s/he has logged in?
> 
> acts\_as\_authenticated[\*] does this out of the box. However, if you  
> want to write your own system take a look at the method  
> redirect\_back\_or\_default here
> 
> ```
> http://tinyurl.com/m62y5
> 
> ```
> 
> – fxn
> 
> [\*] [http://wiki.rubyonrails.org/rails/pages/Acts\_as\_authenticated](http://wiki.rubyonrails.org/rails/pages/Acts_as_authenticated)

Just curious, is it possible to extend this further such that if the  
original request was a HTTP POST, that POST request and with all its  
form values could be “repeated” after the user logs in?

---

<div class="post-metadata">

**Author:** ![generik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/4da419/32.png) [@generik](https://www.ruby-forum.com/u/generik)\
**Post date:** [March 21, 2006, 12:24pm UTC](https://www.ruby-forum.com/t/remembering-link-to-redirect-to-after-logging-in/84608/5 "2006-03-21T12:24:11Z")

</div>

Woei S. wrote:

> > ```
> > http://tinyurl.com/m62y5
> > 
> > ```
> > 
> > – fxn
> > 
> > [\*] [http://wiki.rubyonrails.org/rails/pages/Acts\_as\_authenticated](http://wiki.rubyonrails.org/rails/pages/Acts_as_authenticated)
> 
> Just curious, is it possible to extend this further such that if the  
> original request was a HTTP POST, that POST request and with all its  
> form values could be “repeated” after the user logs in?

When would you have access to a form but _not_ the URL it posts to?

Jeroen

---

<div class="post-metadata">

**Author:** ![generik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/4da419/32.png) [@generik](https://www.ruby-forum.com/u/generik)\
**Post date:** [March 21, 2006, 12:40pm UTC](https://www.ruby-forum.com/t/remembering-link-to-redirect-to-after-logging-in/84608/6 "2006-03-21T12:40:31Z")

</div>

Jeroen H. wrote:

> When would you have access to a form but _not_ the URL it posts to?
> 
> Jeroen

Using ruby-forum as an example, let’s say I’ve clicked on the “reply”  
and halfway through a reply I went off elsewhere and hours later I came  
back, completed my post, submitted, and realises that my session has  
timed out 🙂

---

<div class="post-metadata">

**Author:** ![generik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/4da419/32.png) [@generik](https://www.ruby-forum.com/u/generik)\
**Post date:** [March 21, 2006, 12:42pm UTC](https://www.ruby-forum.com/t/remembering-link-to-redirect-to-after-logging-in/84608/7 "2006-03-21T12:42:45Z")

</div>

Jeroen H. wrote:

> When would you have access to a form but _not_ the URL it posts to?  
> When your session times out while editing the form?

–  
View this message in context:  
[http://www.nabble.com/"Remembering"-link-to-redirect-to-after-logging-in-t1316628.html#a3511039](http://www.nabble.com/%22Remembering%22-link-to-redirect-to-after-logging-in-t1316628.html#a3511039)  
Sent from the RubyOnRails Users forum at [Nabble.com](http://Nabble.com).

---

<div class="post-metadata">

**Author:** ![generik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/4da419/32.png) [@generik](https://www.ruby-forum.com/u/generik)\
**Post date:** [March 21, 2006, 1:00pm UTC](https://www.ruby-forum.com/t/remembering-link-to-redirect-to-after-logging-in/84608/8 "2006-03-21T13:00:43Z")

</div>

Woei S. wrote:

> However while the filtering function does work correctly in redirecting  
> the user to the login page, how do I actually capture the original  
> request made by the user so that I can redirect the user back to the  
> required resource after s/he has logged in?
> 
> Thanks!  
> See page 130/131 in awdr. There’s an example which works by storing  
> request.parameters in a session variable in your auth filter, then  
> redirecting to it (or :action =\> “index”, should the session variable be  
> nil) on successful login.

–  
View this message in context:  
[http://www.nabble.com/"Remembering"-link-to-redirect-to-after-logging-in-t1316628.html#a3511226](http://www.nabble.com/%22Remembering%22-link-to-redirect-to-after-logging-in-t1316628.html#a3511226)  
Sent from the RubyOnRails Users forum at [Nabble.com](http://Nabble.com).

---

<div class="post-metadata">

**Author:** ![generik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/4da419/32.png) [@generik](https://www.ruby-forum.com/u/generik)\
**Post date:** [March 21, 2006, 1:49pm UTC](https://www.ruby-forum.com/t/remembering-link-to-redirect-to-after-logging-in/84608/9 "2006-03-21T13:49:27Z")

</div>

Lucifron wrote:

> Jeroen H. wrote:
> 
> > When would you have access to a form but _not_ the URL it posts to?  
> > When your session times out while editing the form?

Yes that’s indeed a valid scenario. It’s a rare case though so I  
wouldn’t write any code to handle such a rare case, but that’s just my  
opinion of course 🙂

Jeroen

---

<div class="post-metadata">

**Author:** ![generik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/4da419/32.png) [@generik](https://www.ruby-forum.com/u/generik)\
**Post date:** [March 21, 2006, 1:44pm UTC](https://www.ruby-forum.com/t/remembering-link-to-redirect-to-after-logging-in/84608/10 "2006-03-21T13:44:39Z")

</div>

Lucifron wrote:

> > However while the filtering function does work correctly in redirecting  
> > the user to the login page, how do I actually capture the original  
> > request made by the user so that I can redirect the user back to the  
> > required resource after s/he has logged in?
> > 
> > Thanks!  
> > See page 130/131 in awdr. There’s an example which works by storing  
> > request.parameters in a session variable in your auth filter, then  
> > redirecting to it (or :action =\> “index”, should the session variable be  
> > nil) on successful login.

I’m not sure about storing it in the session. It may well work just  
fine, but I just use a hidden form field to remember the original  
request URI because I think storing stuff like that in the session is  
going to backfire sooner or later. But maybe I’m just being conservative  
😉

Jeroen

---

<div class="post-metadata">

**Author:** ![generik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/4da419/32.png) [@generik](https://www.ruby-forum.com/u/generik)\
**Post date:** [March 21, 2006, 2:32pm UTC](https://www.ruby-forum.com/t/remembering-link-to-redirect-to-after-logging-in/84608/11 "2006-03-21T14:32:23Z")

</div>

Woei S. wrote:

> What’s funnier is the parameters got completely borked
> 
> Parameters: {“user”=\>“usernameasd1lock\_version0password”,  
> “commit”=\>“Edit”, “id”=\>“3”}
> 
> (the username was asd1 and the rest and pretty self explainatory)
> 
> What went wrong here?  
> Looks like a hash got flattened to a string.

{:a =\> ‘1’, :b =\> ‘2’}.to\_s ==\> ‘a1b2’

You’re not doing an \<%= @params %\> (or something to that effect) in a  
hidden field in your login form, are you?

---

<div class="post-metadata">

**Author:** ![generik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/4da419/32.png) [@generik](https://www.ruby-forum.com/u/generik)\
**Post date:** [March 21, 2006, 2:25pm UTC](https://www.ruby-forum.com/t/remembering-link-to-redirect-to-after-logging-in/84608/12 "2006-03-21T14:25:40Z")

</div>

Jeroen H. wrote:

> Lucifron wrote:  
> Yes that’s indeed a valid scenario. It’s a rare case though so I  
> wouldn’t write any code to handle such a rare case, but that’s just my  
> opinion of course 🙂
> 
> Jeroen

Heh that’s true, it’s nothing serious, I’m just doing it cos I want to  
figure out how it is done.

Ironically it turned out that the solution I hacked out was similar to  
the one in AWDWR but I keep getting an error.

undefined method `stringify\_keys!’ for  
“usernameasd1lock\_version0password”:String

What I did was log in, go to a scaffold page to display an edit form,  
log out, and then submit the edited form. Sure enough I got redirected  
to the login form, but after I logged on it spit out that error.

What’s funnier is the parameters got completely borked

Parameters: {“user”=\>“usernameasd1lock\_version0password”,  
“commit”=\>“Edit”, “id”=\>“3”}

(the username was asd1 and the rest and pretty self explainatory)

What went wrong here?

😕

---

<div class="post-metadata">

**Author:** ![generik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/4da419/32.png) [@generik](https://www.ruby-forum.com/u/generik)\
**Post date:** [March 22, 2006, 10:07am UTC](https://www.ruby-forum.com/t/remembering-link-to-redirect-to-after-logging-in/84608/13 "2006-03-22T10:07:13Z")

</div>

Woei S. wrote:

> Any chance that when I did the assignment to the session variable that  
> Ruby somehow casted the hash into a string?

That’d be my guess, but I haven’t the code here to check. You could  
try:

```
session[:forward] = YAML::dump(params)

```

and then:

```
redirect_to YAML::load(session[:forward])

```

or something along those lines. There may well be a slicker way, but  
that should, at least, stop string casting from getting in the way.

–  
Alex

---

<div class="post-metadata">

**Author:** ![generik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/4da419/32.png) [@generik](https://www.ruby-forum.com/u/generik)\
**Post date:** [March 21, 2006, 10:12pm UTC](https://www.ruby-forum.com/t/remembering-link-to-redirect-to-after-logging-in/84608/14 "2006-03-21T22:12:30Z")

</div>

Alex Y. wrote:

> Woei S. wrote:
> 
> > What’s funnier is the parameters got completely borked
> > 
> > Parameters: {“user”=\>“usernameasd1lock\_version0password”,  
> > “commit”=\>“Edit”, “id”=\>“3”}
> > 
> > (the username was asd1 and the rest and pretty self explainatory)
> > 
> > What went wrong here?  
> > Looks like a hash got flattened to a string.
> 
> {:a =\> ‘1’, :b =\> ‘2’}.to\_s ==\> ‘a1b2’
> 
> You’re not doing an \<%= @params %\> (or something to that effect) in a  
> hidden field in your login form, are you?

Nope, what I did was to store request.parameters into a session variable  
when the user is redirected and then attempt to do a redirect to that  
stored hash value when the user has logged in.

Any chance that when I did the assignment to the session variable that  
Ruby somehow casted the hash into a string?
