7stud -- wrote in post #1013962:
> The part that really confused me on p. 345 is where the
> author says:
>
> ==
> self.current_user = user
>
> The purpose of this line is to create current_user, accessible in both
> controllers and views which will allow constructions such as:
>
> <%= current_user.name %>
>
> and
>
> redirect_to current_user
> ==
>
> The author says "current_user" not 'the current_user function".
Actually, in the line:
self.current_user = user
the function being called is named 'current_user='. ruby syntax just
permits you to write it with a space before the equals sign.
It's hard to tell if its just a typo when the author says
'current_user', and the author really means '@current_user'. Then there
is this final statement on the page:
==
The principal goal of this section is to define current_user.
==
A current_user getter method??! What's that got to with the line:
> self.current_user = user
I'm going to go with the following 'translations' (because it's the only
way things make sense to me):
On page 345, this part:
===
self.current_user = user
The purpose of this line is to create current_user.
===
should say:
==
The purpose of this line is to to create @current_user.
==
And this introduction:
==
The principal goal of this section is to define current_user.
==
Should be clarified by saying:
==
The principal goal of this section is to define the current_user getter
function.
==
Also I said this:
> Now the question is why use self in the sign_in method here:
>
> def sign_in
> ...
> self.current_user = user
> end
>
> when you can avoid all that confusing stuff and just write:
>
> def sign_in
>
> @current_user = user
> end
>
>
> The answer is it's good practice to always use an accessor method to
> access an instance variable--rather than assign directly to an instance
> variable.
>
In that context, let's look at the current_user function that the
author came up with:
def current_user
@current_user ||= user_from_remember_token
end
I see two problems with that function. First off, that is a getter
function, yet it can set the @current_user variable. Getter functions
should not set instance variables. Secondly, the function directly sets
the @current_user variable instead of calling the current_user= setter
function.
Looking at the application code, i.e. the create action, nothing even
calls the current_user getter method. The create action calls the
sign_in helper method, but sign_in doesn't call current_user. The only
thing that calls current_user is one of the tests. So we're testing
something that the application code doesn't even use???
In my opinion, the current_user getter method as defined in the book is
a complete disaster: breaking it up into three methods is confusing, a
getter method shouldn't set an @ variable, and an @ variable should be
set indirectly by calling the @ variable's setter method.
--
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@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
No comments:
Post a Comment