Nested routes in 2.0.2

Hi. I have 3 tables, forum, post and reply.
I would like to access replies with the url forums/x/posts/x/replies/x
....

I have the posts nested within the forums, but if I wanted to add
another layer, would I have to do this on a separate line in the routes
file or simply add :has_many => :replies to what I already have:

map.resources :forums, :has_many => :posts

Thanks in advance!

Is there any reason why you want to do THREE level deep nested routes? Jamis Buck provided a fairly good argument against this, last year: http://weblog.jamisbuck.org/2007/2/5/nesting-resources.

If you’re still interested in doing it and I can’t disuade you:

map.resources :forums do |forum|
forum.resources :posts do |post|
post.resources :replies
end
end

Again, I must warn you that this leads to really ugly URLs. It’s best to only do two level deep nested routes, as in forums/1/posts and posts/1/replies/1, makes your URLs shorter and prettier.

Thanks for the reply. OK, I can add a reply to a post by using a
partial, this takes away the need to nest the routes.

How can I access the id of the current post and then pass it to the
reply controller to fill the post_id field?

Thx.

Thanks for the reply. OK, I can add a reply to a post by using a
partial, this takes away the need to nest the routes.

How can I access the id of the current post and then pass it to the
reply controller to fill the post_id field?

You can call a before_filter to initialize the post object:

before_filter :find_post

and then further down (at the bottom) of your controller put:

private
def find_post
@post = Post.find(params[:post_id]) if params[:post_id]
end

And that’ll initalize a post object every time the post_id has been specified.

To create a reply to that post, do @post.replies.build(params[:reply]) and that will automatically pass in the post_id.

Hi. I understand what you're saying but for some reason the value isn't
being passed from the url to the reply controller.

You have a nil object when you didn't expect it!
The error occurred while evaluating nil.replies