user3546559
user3546559

Reputation: 43

For the ruby on rails guide, need help on understanding how the render works for partial

I've been going through the ruby on rails guide but I noticed something that I cant seem to understand. At the section where it teaches you how to render a partial form for the comment section of the blog. For the partial comments, they used

<h2>Comments</h2> <%= render @article.comments %>

but for the form they used

<h2>Add a comment:</h2>
<%= render "comments/form" %>

Why is it that the first one used @article and not render "/comments"

Upvotes: 0

Views: 54

Answers (2)

Babar
Babar

Reputation: 1202

It is because it will render the comments partial for each comment on the article object. It's like a 'for' loop without having to know the count of the comments, RAILS takes care of that. For example: If an article has 2 comments it will render the partial twice.

You can read more about object associations in rails documentation.

Upvotes: 0

Saurabh
Saurabh

Reputation: 73609

Here the difference is, @article.comments is a variable from @article table entry, however comments/form will be a html.erb file by the name _form.html.erb in comments folder.

You can find various use of render on rubyonrails page here.

Upvotes: 3

Related Questions