# Nested routes in 2.0.2

**URL:** <https://www.ruby-forum.com/t/nested-routes-in-2-0-2/132045>\
**Category:** Rails\
**Created:** [March 15, 2008, 9:58pm UTC](https://www.ruby-forum.com/t/nested-routes-in-2-0-2/132045 "2008-03-15T21:58:49Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![drsharp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e9bcb4/32.png) [@drsharp](https://www.ruby-forum.com/u/drsharp)\
**Post date:** [March 15, 2008, 9:58pm UTC](https://www.ruby-forum.com/t/nested-routes-in-2-0-2/132045/1 "2008-03-15T21: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:** ![drsharp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e9bcb4/32.png) [@drsharp](https://www.ruby-forum.com/u/drsharp)\
**Post date:** [March 16, 2008, 2:03am UTC](https://www.ruby-forum.com/t/nested-routes-in-2-0-2/132045/2 "2008-03-16T02:03:21Z")

</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:  
[Buckblog: 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.

On Sun, Mar 16, 2008 at 7:28 AM, Dan S.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> ## Thanks in advance!
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> >

–  
Ryan B.

> **[frozenplague.net](http://www.frozenplague.net)**
>
> This domain may be for sale!

Feel free to add me to MSN and/or GTalk as this email.

---

<div class="post-metadata">

**Author:** ![drsharp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e9bcb4/32.png) [@drsharp](https://www.ruby-forum.com/u/drsharp)\
**Post date:** [March 16, 2008, 3:14am UTC](https://www.ruby-forum.com/t/nested-routes-in-2-0-2/132045/3 "2008-03-16T03: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:** ![drsharp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e9bcb4/32.png) [@drsharp](https://www.ruby-forum.com/u/drsharp)\
**Post date:** [March 16, 2008, 5:08am UTC](https://www.ruby-forum.com/t/nested-routes-in-2-0-2/132045/4 "2008-03-16T05:08:35Z")

</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.

On Sun, Mar 16, 2008 at 12:44 PM, Dan S. \<  
removed\_email\_address@domain.invalid\> wrote:

> >

–  
Ryan B.

> **[frozenplague.net](http://www.frozenplague.net)**
>
> This domain may be for sale!

Feel free to add me to MSN and/or GTalk as this email.

---

<div class="post-metadata">

**Author:** ![drsharp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e9bcb4/32.png) [@drsharp](https://www.ruby-forum.com/u/drsharp)\
**Post date:** [March 16, 2008, 4:09pm UTC](https://www.ruby-forum.com/t/nested-routes-in-2-0-2/132045/5 "2008-03-16T16: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

---

<div class="post-metadata">

**Author:** ![drsharp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e9bcb4/32.png) [@drsharp](https://www.ruby-forum.com/u/drsharp)\
**Post date:** [March 16, 2008, 3:20am UTC](https://www.ruby-forum.com/t/nested-routes-in-2-0-2/132045/6 "2008-03-16T03:20:42Z")

</div>

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
