# How can i redirect a person after a login

**URL:** <https://www.ruby-forum.com/t/how-can-i-redirect-a-person-after-a-login/95437>\
**Category:** Rails\
**Created:** [June 1, 2006, 8:00am UTC](https://www.ruby-forum.com/t/how-can-i-redirect-a-person-after-a-login/95437 "2006-06-01T08:00:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [June 1, 2006, 8:00am UTC](https://www.ruby-forum.com/t/how-can-i-redirect-a-person-after-a-login/95437/1 "2006-06-01T08:00:20Z")

</div>

i built my own login system, but unfortunately, i am having problems  
(ie, can’t really figure out how) to redirect a person back to the page  
he was requesting before logging in…say, as the admin i want to edit a  
certain message/logo, whatever, so i click on it, and i get redirected  
to my login page. after the admin logs in, he gets redirected to the  
homepage - i couldn’t figure out how to redirect to the page he  
requested…  
my loggin system is this:

APPLICATION CONTROLLER:

## def secure redirect\_to :controller =\> ‘login’ unless @session[:admin] == 1 end

in my LOGIN CONTROLLER:

## def authenticate pswd = [‘shai’, ‘octava’] @input = [“#{params[:user][:name]}”, “#{params[:user][:password]}” ] if pswd == @input @session[:admin] = 1 flash[:notice] = ‘successful entry.’ redirect\_to :controller =\> ‘messages’ ####THIS IS WHAT I NEED TO FIX else @session[:admin] = 0 flash[:notice] = ‘× ×ª×?× ×?× ×©×?×?×?×?×, ×× × × ×¡×? ×©× ×?×ª.’ redirect\_to :action =\> ‘login’ end end

(i put the secure method in any action i wish to protect, and walla!,  
everythings good)(except the redirecting issue)  
i know the answer should be something like @session[:url\_path] = xyz,  
and then redirect in the authenticate action to @session[:url\_path], but  
the problem is, i’m not really sure what the xyz part is (that stores  
the current url the person is on) and i aint really sure what the  
correct syntax for doing this is…

if anyone has an answer, clue, or just plain sympathy, i’ll be ready;  
thanks.  
hola,

shai  
octava

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [June 1, 2006, 8:06am UTC](https://www.ruby-forum.com/t/how-can-i-redirect-a-person-after-a-login/95437/2 "2006-06-01T08:06:43Z")

</div>

> ```
> redirect_to :controller => 'messages' ####THIS IS WHAT I NEED TO 
> 
> ```
> 
> FIX

redirect\_back\_or\_default is probably what you want. check  
acts\_as\_authenticated for a use case. it only seems to actualy redirect  
back when your access to a page was ‘interrupted’ for needing login, say  
youre on a page that normal users can only view, and special users can  
additioanlly delete or add items, if you click on a link to  
/account/login youll never come back to that page after logging in,  
unless im missing something…

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [June 1, 2006, 8:41am UTC](https://www.ruby-forum.com/t/how-can-i-redirect-a-person-after-a-login/95437/3 "2006-06-01T08:41:36Z")

</div>

-========detailed=========-

this is the way the admin is redirected:

* * *

## def secure redirect\_to :controller =\> ‘login’ unless @session[:admin] == 1 end

and then i implement the secure method in any one of the actions i want  
to protect, such as ‘edit’:

def edit  
secure  
…  
end

and then, when a person who is on the page  
[http://localhost:3000/messages](http://localhost:3000/messages) wants to edit a message, say  
[http://localhost:3000/edit/3](http://localhost:3000/edit/3), he will automaticly be redirected to  
[http://localhost:3000/login/login](http://localhost:3000/login/login), where in this action there will be a  
user, and pswd field, and if he enters them correctly (which you can see  
in the authenticate action, is ‘shai’ and ‘octava’) he will be  
redirected to :controller =\> ‘messages’ (above where i wrote ‘#WHERE I  
NEED TO FIX’). this is the place i need to somehow, (if possible??),  
insert some kind of a parameter/variable that will give me the  
path/url/action-conroller/whatever of the last page visited before the  
‘redirection’ to the login page = = = i.e, after entering the user+pswd,  
get redirected back to (examply obviously) [http://localhost:3000/edit/3](http://localhost:3000/edit/3).

is there any way to do this??  
(ps thanks for the reply)

shai

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [June 1, 2006, 5:03pm UTC](https://www.ruby-forum.com/t/how-can-i-redirect-a-person-after-a-login/95437/4 "2006-06-01T17:03:15Z")

</div>

I think a better to do is with before\_filter call back function.

for example

def secure  
redirect\_to :controller =\> ‘login’ unless @session[:admin] == 1  
end

Then in your edit controller

class EditController \< ApplicationController  
before\_filter :secure, :except =\> :login #except is a method that  
doesn’t require login (you can put more in the except)  
…

Hope this helps 🙂

Victor
