Jean-David
2012-Oct-23 13:11 UTC
A general question about railstutorial.org sessions helper methods
Hi guys, As you probably guess I''m pretty new to RoR. I''m going through railstutorial.org, which I would recommend to other beginners like me, but at some point in chapter 8 there''s a bit of code I don''t really get in the sessions helper. Here''s the code I''m talkng about : module SessionsHelper def sign_in(user) cookies.permanent[:remember_token] = user.remember_token self.current_user = user end def current_user=(user) @current_user = userend def current_user @current_user ||= User.find_by_remember_token(cookies[:remember_token])end end What does self.current_user = user do? It calls the current_user=(user) method passing user as a parameter, right? But do we need the user parameter? I got rid of it and it works fine, as far as I can see. What does current_user=(user) do? And what/who calls current_user when? ... I''m kind of confused. If someone could explain that a bit, I would appreciate. Thx. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/ewyhx34_D4YJ. For more options, visit https://groups.google.com/groups/opt_out.
Frederick Cheung
2012-Oct-23 16:22 UTC
Re: A general question about railstutorial.org sessions helper methods
On Tuesday, October 23, 2012 2:11:31 PM UTC+1, Jean-David wrote:> > module SessionsHelper > > def sign_in(user) > cookies.permanent[:remember_token] = user.remember_token > self.current_user = user > end > > def current_user=(user) > @current_user = userend > > def current_user > @current_user ||= User.find_by_remember_token(cookies[:remember_token])end > > end > > > What does self.current_user = user do? > It calls the current_user=(user) method passing user as a parameter, > right? But do we need the user parameter? I got rid of it and it works > fine, as far as I can see. > > > What does current_user=(user) do? > And what/who calls current_user when? >Typically you''re calling current_user, for example to check whether there is a logged in user, so that you know what user is creating comments, posts etc. As I read it, self.current_user= is just a bit of an optimisation - if the user has just signed in then you know what the current_user is so you can prime the cache (@current_user) with the user value you''ve been passed Fred -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/g1YkspqA7wUJ. For more options, visit https://groups.google.com/groups/opt_out.
Jean-David
2012-Oct-24 15:19 UTC
Re: A general question about railstutorial.org sessions helper methods
Hmm I''m still confused. But thx for helping. Le mardi 23 octobre 2012 18:22:12 UTC+2, Frederick Cheung a écrit :> > > On Tuesday, October 23, 2012 2:11:31 PM UTC+1, Jean-David wrote: >> >> module SessionsHelper >> >> def sign_in(user) >> cookies.permanent[:remember_token] = user.remember_token >> self.current_user = user >> end >> >> def current_user=(user) >> @current_user = userend >> >> def current_user >> @current_user ||= User.find_by_remember_token(cookies[:remember_token])end >> >> end >> >> >> What does self.current_user = user do? >> It calls the current_user=(user) method passing user as a parameter, >> right? But do we need the user parameter? I got rid of it and it works >> fine, as far as I can see. >> >> >> What does current_user=(user) do? >> And what/who calls current_user when? >> > > Typically you''re calling current_user, for example to check whether there > is a logged in user, so that you know what user is creating comments, posts > etc. > > As I read it, self.current_user= is just a bit of an optimisation - if the > user has just signed in then you know what the current_user is so you can > prime the cache (@current_user) with the user value you''ve been passed > > Fred >-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/GkmeiAA5dlUJ. For more options, visit https://groups.google.com/groups/opt_out.
Kevin McCaughey
2012-Oct-24 16:21 UTC
Re: A general question about railstutorial.org sessions helper methods
I am just about to finish chapter 7! So many times I have wished I could grab a chat with another person doing the tutorial, it''s a lonely journey I found ;) -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
Jean-David
2012-Oct-24 16:45 UTC
Re: A general question about railstutorial.org sessions helper methods
Hi Ruby-Forum.com User, You''re not alone anymore... it''s really a small world after all! Good to know there''s someone out there going through the same pain as you do :) I''m using google talk, feel free to add me to your contacts. Le mercredi 24 octobre 2012 18:23:12 UTC+2, Ruby-Forum.com User a écrit :> > I am just about to finish chapter 7! So many times I have wished I could > grab a chat with another person doing the tutorial, it''s a lonely > journey I found ;) >-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/QPnKbSmSpLcJ. For more options, visit https://groups.google.com/groups/opt_out.