form_for arguments

At the following:

Under: 7.4 Generating a Controller

It mentions the following:

<%= form_for([@post,]) do |f| %>

Why should we insert TWO arguments?

Doesn't do the idea? Since I need a form for the
comments belonging to @post?

What is the use of the FIRST @post?

It seems that we are passing an array here?


It's because comment is setup as a resource nested within post.

The form_for needs to generate the right combination of url and http
verb, and this depends on whether or not the comment is a new record.
The form_for helper is designed to allow the same form to be used both
for the new and edit views.

Passing an array tells form_for that it's a nested resource.

In this case, will be a new record, it won't have
an id until it's saved. Let's say that the id of @post is 42. Then
the form should be set up to send a post request using the uri path

If it was the edit case then the comment would have an id, say 432,
and you'd use something like

  form_for @post, @comment

where the edit action in the controller set both instance variables,
and the form would send a put request to the url

If you just gave form_for the single value then
it assumes that the comment resources isn't nested and it would post
to the uri path 'comments' but the way the routes are specified this
isn't going to work.

Does that make sense?

Thanks s lot Rick for this nice explnation. Yes, does make sense.


Just a small note under "links" at your blog, it seems tht the
"Redhanded" is broken.


Seems this is broken also at your LinkedIn:

This is beside "Websites".