How to get current logged in user''s session id? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Patrik.Hedman-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Dec-26 16:51 UTC
Re: current user''s session id?
Hi, session[:id] holds the current user''s id, although you have to set it manually when the user logs in to your site, e.g session[:id] @user.id Hope that helps. On Dec 26, 10:37 am, "Vapor .." <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> How to get current logged in user''s session id? > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Dec 26, 2007, at 10:51 AM, Patrik.Hedman-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote:> > Hi, > > session[:id] holds the current user''s id, although you have to set it > manually when the user logs in to your site, e.g session[:id] > @user.id >To be a bit more clear, you can use whatever you want in the session, it doesn''t have to be :id. It can be :user_id, :logged_in_user_id, whatever. But Patrick is correct in that the value has to get set when the user is logged in. If you''re using something like acts_as_authenticated, it handles all of that for you and you can use current_user.id But if you''re rolling your own like I do, then you set the session variable when the user successfully authenticates. Something like: # find user by email and password user = User.find_by_email_and_password(params[:email], params [:password]) if user session[:user_id] = user.id redirect_to :controller => ''user'', :action => ''home'' else flash[:error] = "Invalid login!" redirect_to :action => ''login'' end Peace, Phillip --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Phillip Koebbe wrote:> On Dec 26, 2007, at 10:51 AM, Patrik.Hedman-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote: >> Hi, >> >> session[:id] holds the current user''s id, although you have to set it >> manually when the user logs in to your site, e.g session[:id] >> @user.id> # find user by email and password > user = User.find_by_email_and_password(params[:email], params > [:password]) > if user > session[:user_id] = user.id > redirect_to :controller => ''user'', :action => ''home'' > else > flash[:error] = "Invalid login!" > redirect_to :action => ''login'' > end > > Peace, > Phillipvery helpful, thanks :) -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---