Ruby on Rails Sunday, July 31, 2011

On Jul 31, 7:41 pm, Filippos <filippos...@gmail.com> wrote:
> Ok, I'm not as advanced as 7stud. Im not familiar with ruby and I
> started studying Rails.
> Your replies where very informative but also a bit confusing at the
> same time.
>
> 1. So when we want to go to localhost/myapp/users/1 rails first maps
> the URL to Users#show, then creates the object user_controller =
> UsersController.new and then executes the user_controller.show?
>
basically

> 2. When we assign an instance variable inside the controller , doesn't
> it refer to the controller object that rails create? For example @user
> -> user_controller = UsersController.new
> What would the difference be between the instance variable and the
> self.user inside t he controller method.
>

I think you may be imagining magic that doesn't exist.

> 3. I didn't understand your justification of getter and setter.Since
> instance variables work fine why would you need to define a setter and
> getter. You can just assign to the instance variable and you're done
> since it will be accessible in the controller and view for as long as
> the sess_controller lasts.
>
> Instead of self.current_user = user we could write @current_user =
> user
>
> Why call the current_user getter and not just call the @current_user
> directly.
>

For me the advantage of a current_user method over using @current_user
directly is that I don't have to worry about whether I've setup
@current_user already or not, I can just call current_user and things
will not. Equally if I never need to check current_user then I don't
do the work to set that up unnecessarily. That the current_user method
happens to cache its result in the instance variable of the same name
is just an implementation detail.

Fred


> I'm a bit confused.
>
> The easy way would be to memorize or copy/paste but understanding the
> code is better way to create rails apps.

--
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