Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8152229
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T15:39:01+00:00 2026-06-06T15:39:01+00:00

Following ROR’s guides getting started tutorial , I am making a blog having two

  • 0

Following ROR’s guides getting started tutorial, I am making a blog having two models – Post & Comment – with following associations in config/routes.rb file:

resources :posts do
  resources :comments
end

As such, comments and comment form is shown on the blog’s view/posts/show.html.erb page for display reasons. However, I am stuck in understanding what does @post, @post.comments.build means in the following snippet of code:

<h2>Add a comment:</h2>
<%= form_for([@post, @post.comments.build]) do |f| %>
  <div class="field">
    <%= f.label :commenter %><br />
    <%= f.text_field :commenter %>
  </div>
  <div class="field">
    <%= f.label :body %><br />
    <%= f.text_area :body %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

Also, during re-factoring, the author has moved code to display comments to the view/comments/_comment.html.erb partial and has rendered it in view/posts/show.html.erb using

<%= render @post.comments %>

This will now render the partial in app/views/comments/_comment.html.erb once for each comment that is in the @post.comments collection. As the render method iterates over the @post.comments collection, it assigns each comment to a local variable named the same as the partial, in this case comment which is then available in the partial for us to show.

Q: How does rails infer the partial name and the variable to be passed to the partial for displaying comments from above line of code?

Also, can you explain what does the following statement means in terms of the above mentioned blog application. Ref: http://guides.rubyonrails.org/getting_started.html

The @post object is available to any partials rendered in the view because we defined it as an instance variable.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T15:39:02+00:00Added an answer on June 6, 2026 at 3:39 pm

    First question.

    What does [@post, @post.comments.build] mean?

    Basically this is instantiating what is going to be produced in your form. The @post, and it’s also going to build @post.comments. But this seems unnecessary, and I think you could accomplish the same with just :

    <%= form_for @post do |f| %>
    

    Unless the form it self on producing the form, also gives you the chance to write comments at the exact same time.

    Second question.

    The way partials work is that so long as they’re the same name as the object. In your case _comment.html.erb is the same name as your model Comment. So Rails will auto-magically assume that is what you are looking for.

    If you were to change the name of your partial to something else, you’d write more specifically this :

    <%= render :partial => 'comments/sweet_comment_partial_name', :collection => @post.comments %>
    

    Update

    Ah ok I didn’t realize what form you were referring to. This one specifically that instantiates this :

    [@post, @post.comments.build]
    

    Is referring to the Comment model. But its saying that the comment’s parent is @post. So that the comment model you’re specifically referring to is the child of @post, and is going to be built on or appended to your array of comments that is associated with that @post.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

After following the RoR getting started tutorial, I added another model as: $ rails
I created a blog by following the Getting Started with Rails precisely. And then
Im following a tutorial online (http://my.opera.com/learnror/blog/ldap-binding-and-authentication-ror) to setup authentication against an LDAp active directory
First attempt at a RoR project and following http://guides.rubyonrails.org/getting_started.html#creating-the-blog-application . I've got the Welcome
I am following Michael's ROR tutorial and creating user authentication system. There is an
I am currently following Michael Hartl's tutorial on RoR and am trying to get
I currently have the following models/toy.rb file in my RoR project: class ToysPurchased include
I'm currently going through the RoR guides, and i'm stuck at... Adding following/follower relationships
I thought I was following this RoR tutorial to a T, but apparently not.
I was following the screencast on rubyonrails.org (creating the blog). I have following models:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.