# Nested routes in 2.0.2

**URL:** <https://discuss.rubyonrails.org/t/nested-routes-in-2-0-2/26372>\
**Category:** rubyonrails-talk\
**Created:** [March 15, 2008, 8:58pm UTC](https://discuss.rubyonrails.org/t/nested-routes-in-2-0-2/26372 "2008-03-15T20:58:49Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [March 15, 2008, 8:58pm UTC](https://discuss.rubyonrails.org/t/nested-routes-in-2-0-2/26372/1 "2008-03-15T20:58:49Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![radar](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/radar/32/604_2.png) [@radar](https://discuss.rubyonrails.org/u/radar)\
**Post date:** [March 16, 2008, 1:01am UTC](https://discuss.rubyonrails.org/t/nested-routes-in-2-0-2/26372/2 "2008-03-16T01:01:59Z")

</div>

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](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.

---

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [March 16, 2008, 2:14am UTC](https://discuss.rubyonrails.org/t/nested-routes-in-2-0-2/26372/3 "2008-03-16T02:14:32Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Craig\_White2](https://avatars.discourse-cdn.com/v4/letter/c/76d3ee/32.png) [@Craig\_White2](https://discuss.rubyonrails.org/u/Craig_White2)\
**Post date:** [March 16, 2008, 2:20am UTC](https://discuss.rubyonrails.org/t/nested-routes-in-2-0-2/26372/4 "2008-03-16T02:20:33Z")

</div>

> 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?

---

<div class="post-metadata">

**Author:** ![radar](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/radar/32/604_2.png) [@radar](https://discuss.rubyonrails.org/u/radar)\
**Post date:** [March 16, 2008, 4:07am UTC](https://discuss.rubyonrails.org/t/nested-routes-in-2-0-2/26372/5 "2008-03-16T04:07:36Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [March 16, 2008, 3:09pm UTC](https://discuss.rubyonrails.org/t/nested-routes-in-2-0-2/26372/6 "2008-03-16T15:09:40Z")

</div>

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
