# Routing error in nested resources

**URL:** <https://www.ruby-forum.com/t/routing-error-in-nested-resources/164324>\
**Category:** Rails\
**Created:** [March 31, 2009, 8:20am UTC](https://www.ruby-forum.com/t/routing-error-in-nested-resources/164324 "2009-03-31T08:20:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![neubyr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/c37758/32.png) [@neubyr](https://www.ruby-forum.com/u/neubyr)\
**Post date:** [March 31, 2009, 8:20am UTC](https://www.ruby-forum.com/t/routing-error-in-nested-resources/164324/1 "2009-03-31T08:20:48Z")

</div>

I am using nested resources as follows:  
map.resources :topics do |topics|  
topics.resources :items do |items|  
items.resources :attachments  
end  
end

When I generate a RESTful path for new item in the console:  
app.new\_topic\_item\_path(2) then I get correct path as  
‘/topics/2/items/new’.

However, for the new attachment I am getting error:  
app.new\_topic\_item\_attachment\_path(22) gives -  
ActionController::RoutingError: new\_topic\_item\_attachment\_url failed to  
generate from {:action=\>“new”, :controller=\>“attachments”,  
:topic\_id=\>22}, expected: {:controller=\>“attachments”, :action=\>“new”},  
diff: {:topic\_id=\>22}

```
    from

```

/usr/lib/ruby/gems/1.8/gems/actionpack-2.1.0/lib/action\_controller/routing/route\_set.rb:375:in  
`raise_named_route_error' from /usr/lib/ruby/gems/1.8/gems/actionpack-2.1.0/lib/action_controller/routing/route_set.rb:339:in`generate’  
from  
/usr/lib/ruby/gems/1.8/gems/actionpack-2.1.0/lib/action\_controller/url\_rewriter.rb:131:in  
`rewrite_path' from /usr/lib/ruby/gems/1.8/gems/actionpack-2.1.0/lib/action_controller/url_rewriter.rb:110:in`rewrite\_url’  
from  
/usr/lib/ruby/gems/1.8/gems/actionpack-2.1.0/lib/action\_controller/url\_rewriter.rb:88:in  
`rewrite' from /usr/lib/ruby/gems/1.8/gems/actionpack-2.1.0/lib/action_controller/integration.rb:218:in`url\_for’  
from (eval):17:in `new\_topic\_item\_attachment\_path’  
from (irb):5.

* * *

Why is it failing and how to fix this?  
And, why is it generating ‘{:action=\>“new”, :controller=\>“attachments”,  
:topic\_id=\>22}’ ? Shouldn’t it pass item\_id rather than topic\_id?

Any clues?

- 

Thanks,  
CS.

---

<div class="post-metadata">

**Author:** ![neubyr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/c37758/32.png) [@neubyr](https://www.ruby-forum.com/u/neubyr)\
**Post date:** [March 31, 2009, 8:27am UTC](https://www.ruby-forum.com/t/routing-error-in-nested-resources/164324/2 "2009-03-31T08:27:14Z")

</div>

Carlos S. wrote:

> I am using nested resources as follows:  
> map.resources :topics do |topics|  
> topics.resources :items do |items|  
> items.resources :attachments  
> end  
> end
> 
> When I generate a RESTful path for new item in the console:  
> app.new\_topic\_item\_path(2) then I get correct path as  
> ‘/topics/2/items/new’.
> 
> However, for the new attachment I am getting error:  
> app.new\_topic\_item\_attachment\_path(22) gives -  
> ActionController::RoutingError: new\_topic\_item\_attachment\_url failed to  
> generate from {:action=\>“new”, :controller=\>“attachments”,  
> :topic\_id=\>22}, expected: {:controller=\>“attachments”, :action=\>“new”},  
> diff: {:topic\_id=\>22}
> 
> ```
> from
> 
> ```
> 
> /usr/lib/ruby/gems/1.8/gems/actionpack-2.1.0/lib/action\_controller/routing/route\_set.rb:375:in  
> `raise_named_route_error' from /usr/lib/ruby/gems/1.8/gems/actionpack-2.1.0/lib/action_controller/routing/route_set.rb:339:in`generate’  
> from  
> /usr/lib/ruby/gems/1.8/gems/actionpack-2.1.0/lib/action\_controller/url\_rewriter.rb:131:in  
> `rewrite_path' from /usr/lib/ruby/gems/1.8/gems/actionpack-2.1.0/lib/action_controller/url_rewriter.rb:110:in`rewrite\_url’  
> from  
> /usr/lib/ruby/gems/1.8/gems/actionpack-2.1.0/lib/action\_controller/url\_rewriter.rb:88:in  
> `rewrite' from /usr/lib/ruby/gems/1.8/gems/actionpack-2.1.0/lib/action_controller/integration.rb:218:in`url\_for’  
> from (eval):17:in `new\_topic\_item\_attachment\_path’  
> from (irb):5.
> 
> * * *
> 
> Why is it failing and how to fix this?  
> And, why is it generating ‘{:action=\>“new”, :controller=\>“attachments”,  
> :topic\_id=\>22}’ ? Shouldn’t it pass item\_id rather than topic\_id?
> 
> Any clues?
> 
> - 
> 
> Thanks,  
> CS.

Figured it out:  
I did new\_topic\_item\_attachment\_path and got an error:  
‘‘ActionController::RoutingError: new\_agenda\_item\_attachment\_url failed  
to generate from {:action=\>“new”, :controller=\>“attachments”} - you may  
have ambiguous routes, or you may need to supply additional parameters  
for this route. content\_url has the following required parameters:  
[“agendas”, :agenda\_id, “items”, :item\_id, “attachments”, “new”] - are  
they all satisfied?’’

So I need to pass both topic and item id. But isn’t item associated with  
the topic resource? Why do I need to pass both ids?

---

<div class="post-metadata">

**Author:** ![neubyr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/c37758/32.png) [@neubyr](https://www.ruby-forum.com/u/neubyr)\
**Post date:** [March 31, 2009, 5:07pm UTC](https://www.ruby-forum.com/t/routing-error-in-nested-resources/164324/3 "2009-03-31T17:07:26Z")

</div>

Hi Carlos!

Because of the nesting. For example, I recently hacked up a version of  
Beast which is nested similarly to yours: Forums with many Topics with  
many Posts. Creating a new post has the following resource path: /  
forums/:forum\_id/topics/:topic\_id/posts/new  
Even though the topic\_id in my case is globally unique, it still needs  
the forum\_id for that path.

You don’t have to do that, though. You could leave them as non-nested  
and then just use associations, i.e. Topics has\_many Items

I like nested resources conceptually but they do make url generation a  
little confusing.

Just remember to use: “rake routes” liberally so that you always know  
what routes are available and how to map them.

🙂

-Danimal

On Mar 31, 12:27 am, Carlos S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
