views:

76

answers:

2

I have a comment model, I've seen examples of using @comment, :comment, comment to reference the object in MVC. how do I know which is which? Is there a distinction?

+4  A: 
John Topley
Apologies, John. I felt it needed a little bit more explanation.
Ryan Bigg
No need to apologise.
John Topley
This is amazing, this should be explained more on ruby tutorials and books. Now my rails world is getting smaller towards mastery. Thanks!
Winston
+2  A: 

It really depends on the context.

The first one, @comment is usually used (as John Topley similarly explained) for setting up a variable to be passed down the request chain to your views, helpers and partials that are rendered from that action.

If you're using it in a form_for you would reference the @comment object you most likely set up in the controller:

<% form_for @comment do |f| %>

If you're passing a comment object as a local to a partial you could specify it as a symbol (as John Topley said):

<%= render :partial => "info", :locals => { :comment => @comment }

And if you were using it as a local variable you may be rendering a collection of comments:

<%= render :partial => @post.comments %>

Passing the comments collection to partial will introspect upon the first object in here and determine it is of the Comment class and therefore will try to render the comments/_comment.html.erb partial for each of the items in the collection (regardless of what type the others are, this is a gotcha), making them each available as comment inside it.

Ryan Bigg
Thank you! now I have explicit distinctions and understanding!
Winston