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.

On Sun, Mar 16, 2008 at 7:28 AM, Dan S.
[email protected]
wrote:

Thanks in advance!

Posted via http://www.ruby-forum.com/.


Ryan B.
http://www.frozenplague.net
Feel free to add me to MSN and/or GTalk as this email.

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.

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.

On Sun, Mar 16, 2008 at 12:44 PM, Dan S. <
[email protected]> wrote:


Ryan B.
http://www.frozenplague.net
Feel free to add me to MSN and/or GTalk as this email.

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

On Sun, 2008-03-16 at 03:14 +0100, Dan S. wrote:

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?


doesn’t it still show the id in your URL?

:id => params[:id]

you can continue to add params to your form_for and pass them with the
click.

Craig

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs