Form_for helper for nested resources

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

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,]) do |f|
  • form_for @post, :url => new_category_post_comment_path do |f|

What am I doing wrong here!? Thanks guys.

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?

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.

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

<% form_for, :url =>
category_post_comments_path(:category_id => @category, :post_id =>
@post) do |f| %>

This also works, and seems a lot more elegant:

<% form_for [@course, @discussion,] do |f| %>