# Form\_for arguments

**URL:** <https://www.ruby-forum.com/t/form-for-arguments/193309>\
**Category:** Rails\
**Created:** [August 15, 2010, 2:28pm UTC](https://www.ruby-forum.com/t/form-for-arguments/193309 "2010-08-15T14:28:13Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![swengineer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@swengineer](https://www.ruby-forum.com/u/swengineer)\
**Post date:** [August 15, 2010, 2:28pm UTC](https://www.ruby-forum.com/t/form-for-arguments/193309/1 "2010-08-15T14:28:13Z")

</div>

At the following: [Getting Started with Rails — Ruby on Rails Guides](http://edgeguides.rubyonrails.org/getting_started.html)

Under: 7.4 Generating a Controller

It mentions the following:

\<%= form\_for([@post, @post.comments.build]) do |f| %\>

Why should we insert TWO arguments?

Doesn’t @post.comments.build 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?

Thanks.

---

<div class="post-metadata">

**Author:** ![swengineer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@swengineer](https://www.ruby-forum.com/u/swengineer)\
**Post date:** [August 15, 2010, 10:07pm UTC](https://www.ruby-forum.com/t/form-for-arguments/193309/2 "2010-08-15T22:07:54Z")

</div>

On Sun, Aug 15, 2010 at 8:28 AM, Abder-Rahman A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Doesn’t @post.comments.build do the idea? Since I need a form for the  
> comments belonging to @post?
> 
> What is the use of the FIRST @post?

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, @post.comments.build 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  
'posts/42/comments

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  
‘post/42/comments/432’

If you just gave form\_for the single value @post.comments.build 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?

–

Rick DeNatale

Blog: [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)  
Github: [rubyredrick (Rick DeNatale) · GitHub](http://github.com/rubyredrick)  
Twitter: @RickDeNatale  
WWR: [http://www.workingwithrails.com/person/9021-rick-denatale](http://www.workingwithrails.com/person/9021-rick-denatale)  
LinkedIn: [http://www.linkedin.com/in/rickdenatale](http://www.linkedin.com/in/rickdenatale)

---

<div class="post-metadata">

**Author:** ![swengineer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@swengineer](https://www.ruby-forum.com/u/swengineer)\
**Post date:** [August 15, 2010, 10:16pm UTC](https://www.ruby-forum.com/t/form-for-arguments/193309/3 "2010-08-15T22:16:12Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![swengineer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@swengineer](https://www.ruby-forum.com/u/swengineer)\
**Post date:** [August 15, 2010, 10:29pm UTC](https://www.ruby-forum.com/t/form-for-arguments/193309/4 "2010-08-15T22:29:41Z")

</div>

Rick,

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

---

<div class="post-metadata">

**Author:** ![swengineer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@swengineer](https://www.ruby-forum.com/u/swengineer)\
**Post date:** [August 15, 2010, 10:34pm UTC](https://www.ruby-forum.com/t/form-for-arguments/193309/5 "2010-08-15T22:34:21Z")

</div>

Rick,

Seems this is broken also at your LinkedIn:  
[http://speakerrate.com/speaker/395-rdenatale](http://speakerrate.com/speaker/395-rdenatale)

This is beside “Websites”.
