# Form\_for

**URL:** <https://www.ruby-forum.com/t/form-for/193272>\
**Category:** Rails\
**Created:** [August 14, 2010, 3:23pm UTC](https://www.ruby-forum.com/t/form-for/193272 "2010-08-14T15:23:54Z")\
**Posts on this page:** 6\
**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 14, 2010, 3:23pm UTC](https://www.ruby-forum.com/t/form-for/193272/1 "2010-08-14T15:23:54Z")

</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| %\>

Now, this is what I know:

form\_for: used to generate a form.  
@post: the object form\_for is bound to.

The part I didn’t get here is:

@post.comments.build

What does that part provide me with?

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 14, 2010, 4:23pm UTC](https://www.ruby-forum.com/t/form-for/193272/2 "2010-08-14T16:23:21Z")

</div>

On Aug 14, 2:23 pm, Abder-Rahman A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> form\_for: used to generate a form.  
> @post: the object form\_for is bound to.
> 
> The part I didn’t get here is:
> 
> @post.comments.build
> 
> What does that part provide me with?

It’s a namespaced resource - it’s a form for a new comment belonging  
to @post

Fred

---

<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 14, 2010, 4:34pm UTC](https://www.ruby-forum.com/t/form-for/193272/3 "2010-08-14T16:34:27Z")

</div>

Frederick C. wrote:

> On Aug 14, 2:23ï¿½pm, Abder-Rahman A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > form\_for: used to generate a form.  
> > @post: the object form\_for is bound to.
> > 
> > The part I didn’t get here is:
> > 
> > @post.comments.build
> > 
> > What does that part provide me with?
> 
> It’s a namespaced resource - it’s a form for a new comment belonging  
> to @post
> 
> Fred

Thanks Fred.

What is “namespaced resource”?

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 14, 2010, 5:03pm UTC](https://www.ruby-forum.com/t/form-for/193272/4 "2010-08-14T17:03:00Z")

</div>

Thanks Colin.

---

<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 14, 2010, 4:59pm UTC](https://www.ruby-forum.com/t/form-for/193272/5 "2010-08-14T16:59:04Z")

</div>

On 14 August 2010 15:34, Abder-Rahman A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > It’s a namespaced resource - it’s a form for a new comment belonging  
> > to @post
> > 
> > Fred
> 
> Thanks Fred.
> 
> What is “namespaced resource”?

Have a look at the guide on routing, (at  
[http://edgeguides.rubyonrails.org/](http://edgeguides.rubyonrails.org/)) it explains the use of namespaces.

Also google would have given you useful information here again.

Colin

---

<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 19, 2010, 9:16pm UTC](https://www.ruby-forum.com/t/form-for/193272/6 "2010-08-19T21:16:00Z")

</div>

This is a method generated for you by rails as a result of the  
associtions declared in the Post and Comment models - in this case  
belongs\_to and has\_many. Comments belongs to a post, and a post can  
have many comments.

Rails creates quite a number of theses methods in response to the  
association, and different associations (has\_one,  
has\_and\_belongs\_to\_many etc.) create different sets of methods - more  
details in the API docs:

> **[ActiveRecord::Associations::ClassMethods](https://edgeapi.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html)**
>
> Active Record Associations Associations are a set of macro-like class methods for tying objects together through foreign keys.

collection.build is one of these methods, and the API docs explain it  
pretty well:

“collection.build(attributes = {}, …) Returns one or more new  
objects of the collection type that have been instantiated with  
attributes and linked to this object through a foreign key, but have  
not yet been saved.”

“collection” in this case is @post.comments

(None of of which has anything to do with namespaced resources…)

Matt.
