Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails: tracking a user's ID

In my Rails app, I have a login page. After that person logs in, what is the best way for my app to continue tracking the person that has logged in. For example, if the user moves to different pages, my controllers/actions will lose track of that user unless I keep passing a variable between each page the user subsequently visits. Is there a better way of doing this? Should I be using the sessions variable?

like image 455
alamodey Avatar asked Mar 08 '09 10:03

alamodey


3 Answers

Yes, sessions are exactly what you are looking for.

session["user_id"] = user_id

And to fetch the current user on another page (if your model is called User):

@current_user = User.find(session["user_id]")
like image 120
Palm Avatar answered Nov 12 '22 18:11

Palm


Strongly consider a plugin to manage this.

There are several, such as restful authentication.

This gives current_user and logged_in? functionality.

like image 32
wombleton Avatar answered Nov 12 '22 18:11

wombleton


There are some great gems that do this

  • Devise

  • AuthLogic

like image 2
tomblomfield Avatar answered Nov 12 '22 16:11

tomblomfield