# Form\_for helper for nested resources

**URL:** <https://www.ruby-forum.com/t/form-for-helper-for-nested-resources/183531>\
**Category:** Rails\
**Created:** [February 16, 2010, 6:33am UTC](https://www.ruby-forum.com/t/form-for-helper-for-nested-resources/183531 "2010-02-16T06:33:22Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Seirie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b5ac83/32.png) [@Seirie](https://www.ruby-forum.com/u/Seirie)\
**Post date:** [February 16, 2010, 6:33am UTC](https://www.ruby-forum.com/t/form-for-helper-for-nested-resources/183531/1 "2010-02-16T06:33:22Z")

</div>

Man, I have been trying to figure this one out for hours, hopefully  
you guys can clue me in.

Let’s so you have three “nested” models, for example: Category -\> Post  
-\> Comment (that is, many comments belong to one post and many posts  
belong to one category).

So, in your routing configuration you have:

map.resources :categories do |cat|  
cat.resources :posts, :has\_many =\> :comments  
end

The part I cant figure out, is how to write a “form\_for” helper so  
that the HTML action="" attribute will end up being “categories/1/  
posts/1/comments”. The farthest I can seem to get is “posts/1/  
comments”, which, I could proly skate by with, but I’d really like to  
get the entire nested route in their. (I’ve seen that the rails app  
“Basecamp” is able to accomplish this, so it has to be possible).

These are a couple of the things I’ve tried, both result in an error:

- form\_for([:category, @post, Comment.new]) do |f|
- form\_for @post, :url =\> new\_category\_post\_comment\_path do |f|

What am I doing wrong here!? Thanks guys.

---

<div class="post-metadata">

**Author:** ![Seirie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b5ac83/32.png) [@Seirie](https://www.ruby-forum.com/u/Seirie)\
**Post date:** [February 16, 2010, 8:25am UTC](https://www.ruby-forum.com/t/form-for-helper-for-nested-resources/183531/2 "2010-02-16T08:25:43Z")

</div>

Hmmm, maybe I’m thinking about this all wrong. It seems like its grown  
into an even bigger issue now.

If I try to access a nested route from it’s parent resource, I cant.  
I’ll give you an example:

If I try to use \<%= link\_to book\_pages\_path %\> from a page with the  
URL of “books/1”, I get an error saying:

book\_pages\_url failed to generate from  
{:action=\>“index”, :controller=\>“pages”} - you may have ambiguous  
routes, or you may need to supply additional parameters for this  
route. content\_url has the following required parameters:  
[“books”, :book\_id, “pages”] - are they all satisfied?

I just don’t get it…how am I supposed to create a link for users to  
go view ALL the pages for a specific book?

---

<div class="post-metadata">

**Author:** ![Seirie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b5ac83/32.png) [@Seirie](https://www.ruby-forum.com/u/Seirie)\
**Post date:** [February 16, 2010, 8:43am UTC](https://www.ruby-forum.com/t/form-for-helper-for-nested-resources/183531/3 "2010-02-16T08:43:17Z")

</div>

Alright, I figured out the second issue there, I needed to pass in a  
reference to the course in question: \<%= link\_to  
book\_pages\_path(@book) %\>

Man I’m losing it, time for bed, lol.

---

<div class="post-metadata">

**Author:** ![Seirie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b5ac83/32.png) [@Seirie](https://www.ruby-forum.com/u/Seirie)\
**Post date:** [February 16, 2010, 9:01am UTC](https://www.ruby-forum.com/t/form-for-helper-for-nested-resources/183531/4 "2010-02-16T09:01:25Z")

</div>

Ahh, got the first issue as well. For anyone that’s interested, I was  
able to get my desired result with:

\<% form\_for Comment.new, :url =\>  
category\_post\_comments\_path(:category\_id =\> @category, :post\_id =\>  
@post) do |f| %\>

---

<div class="post-metadata">

**Author:** ![Seirie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b5ac83/32.png) [@Seirie](https://www.ruby-forum.com/u/Seirie)\
**Post date:** [February 16, 2010, 9:14am UTC](https://www.ruby-forum.com/t/form-for-helper-for-nested-resources/183531/5 "2010-02-16T09:14:18Z")

</div>

This also works, and seems a lot more elegant:

\<% form\_for [@course, @discussion, Message.new] do |f| %\>
