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
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.
On Tuesday, October 23, 2012 2:11:31 PM UTC+1, Jean-David wrote:
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
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