Re: A general question about railstutorial.org sessions helper methods

28 views
Skip to first unread message

Frederick Cheung

unread,
Oct 23, 2012, 12:22:12 PM10/23/12
to rubyonra...@googlegroups.com

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 = user
end
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 

Jean-David

unread,
Oct 24, 2012, 11:19:44 AM10/24/12
to rubyonra...@googlegroups.com
Hmm I'm still confused.
But thx for helping.

Kevin McCaughey

unread,
Oct 24, 2012, 12:21:47 PM10/24/12
to rubyonra...@googlegroups.com
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/.

Jean-David

unread,
Oct 24, 2012, 12:45:24 PM10/24/12
to rubyonra...@googlegroups.com
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.
Reply all
Reply to author
Forward
0 new messages