tags:

views:

37

answers:

1

What does the [@post, Comment.new] construction literally mean? I'm using it (saw in a podcast, or somewhere else) but I still don't quite understand what does it mean? What literally happens if we hit Submit button (sure, the comment is added, but I'm interesting about the inner mechanics and how is it connected with mentioned construction)?

<% form_for [@post,Comment.new] do |f| %>
   <p>
      <%= f.label :message %><br />
      <%= f.text_area :message %>
   </p>
   <p>
      <%= f.submit 'Post comment' %>
   </p>
<% end %>
A: 

Although it's not clear in the standard documentation, some people have posted helpful comments that clarify what function this serves. Example: http://apidock.com/rails/ActionView/Helpers/FormHelper/form_for

It's to do with generating a route that is either in the correct namespace, or because the path to a particular resource involves two components.

In the case you're citing, the route would be something along the lines of:

/posts/:post_id/comments/

When editing a post's comment:

/posts/:post_id/comments/:id

This contrasts to the route generated for providing Comment.new only:

/comments/
tadman
Thanks! Now that's exactly what I wanted to figure out :)
gmile