# Routing Problem using 2.0.1

**URL:** <https://www.ruby-forum.com/t/routing-problem-using-2-0-1/122555>\
**Category:** Rails\
**Created:** [December 9, 2007, 10:39am UTC](https://www.ruby-forum.com/t/routing-problem-using-2-0-1/122555 "2007-12-09T10:39:54Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![bbebop](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/cdc98d/32.png) [@bbebop](https://www.ruby-forum.com/u/bbebop)\
**Post date:** [December 9, 2007, 10:39am UTC](https://www.ruby-forum.com/t/routing-problem-using-2-0-1/122555/1 "2007-12-09T10:39:54Z")

</div>

I’ve run into a problem upgrading an app to rails 2.0.1. All of this  
worked under 1.2.6, but I’m not sure if the problem is caused by a bug  
or changed behavior in 2.0.1.

this routing:  
map.resources :bands do |t|  
t.resources :instruments  
t.resources :instruments, :collection =\> {:review =\> :get}  
end

generates this when i run rake routes  
review\_band\_instruments GET /bands/:band\_id/instruments/review  
{:action=\>“review”, :controller=\>“instruments”}

this method  
review\_band\_instruments\_path(1)

produces this path  
/bands/1/instruments/review

which erroneously generates these parameters:  
Parameters: {“action”=\>“show”, “id”=\>“review”,  
“controller”=\>“instruments”, “band\_id”=\>“1”}

and this error message:  
ActiveRecord::RecordNotFound (Couldn’t find Source with ID=review AND  
(instruments.band\_id = 1))

under 1.2, this all worked properly with the following parameters:  
Parameters: {“action”=\>“review”, “controller”=\>“instruments”,  
“band\_id”=\>“1”}

Any help appreciated.

---

<div class="post-metadata">

**Author:** ![bbebop](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/cdc98d/32.png) [@bbebop](https://www.ruby-forum.com/u/bbebop)\
**Post date:** [December 9, 2007, 4:04pm UTC](https://www.ruby-forum.com/t/routing-problem-using-2-0-1/122555/2 "2007-12-09T16:04:26Z")

</div>

I’m not sure whether duplication is causing your problem, but I’ve never  
seen anything like this

map.resources :bands do |t|  
t.resources :instruments  
t.resources :instruments, :collection =\> {:review =\> :get}  
end

In the apps that I’ve worked on so far (I’ve only been doing Rails for a  
little over 3 months), my partners and I have only found it necessary to  
declare t.resources once, whether or not we also declare :member and  
:collection.

map.resources :bands do |t|  
t.resources :instruments, :collection =\> {:review =\> :get}  
end

Does that make a difference?

Craig

---

<div class="post-metadata">

**Author:** ![bbebop](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/cdc98d/32.png) [@bbebop](https://www.ruby-forum.com/u/bbebop)\
**Post date:** [December 9, 2007, 7:54pm UTC](https://www.ruby-forum.com/t/routing-problem-using-2-0-1/122555/3 "2007-12-09T19:54:39Z")

</div>

Duplication of resources doesn’t cause the problem. The routes and  
paths get specified properly. The problem occurs when rails translates  
the URLs into the wrong params.

---

<div class="post-metadata">

**Author:** ![bbebop](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/cdc98d/32.png) [@bbebop](https://www.ruby-forum.com/u/bbebop)\
**Post date:** [December 9, 2007, 8:32pm UTC](https://www.ruby-forum.com/t/routing-problem-using-2-0-1/122555/4 "2007-12-09T20:32:03Z")

</div>

bbebop wrote:

> Duplication of resources doesn’t cause the problem. The routes and  
> paths get specified properly. The problem occurs when rails translates  
> the URLs into the wrong params.

Duplication is most certainly the problem. If you do ‘rake routes’  
you’ll see the routes listed in match order. Notice that you have this  
unnamed route:

GET /bands/:band\_id/instruments/:id {:controller=\>“instruments”,  
:action=\>“show”}

above this named route:

review\_band\_instruments GET /bands/:band\_id/instruments/review  
{:controller=\>“instruments”, :action=\>“review”}

Routes match in the order they are defined. However, the second time you  
call t.resources :instruments with the :collection option you re-define  
the named routes. So you are generating the latter route above but  
matching the former, ending up with the #show action. Take out the first  
route and all will be well.

–  
Josh S.  
[http://blog.hasmanythrough.com](http://blog.hasmanythrough.com)

---

<div class="post-metadata">

**Author:** ![bbebop](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/cdc98d/32.png) [@bbebop](https://www.ruby-forum.com/u/bbebop)\
**Post date:** [December 9, 2007, 8:58pm UTC](https://www.ruby-forum.com/t/routing-problem-using-2-0-1/122555/5 "2007-12-09T20:58:00Z")

</div>

Thanks, Josh!

On Dec 9, 2007, at 11:32 AM, Josh S. wrote:

bbebop wrote:

> Duplication of resources doesn’t cause the problem. The routes and  
> paths get specified properly. The problem occurs when rails translates  
> the URLs into the wrong params.

Duplication is most certainly the problem. If you do ‘rake routes’  
you’ll see the routes listed in match order. Notice that you have this  
unnamed route:

GET /bands/:band\_id/instruments/:id {:controller=\>“instruments”,  
:action=\>“show”}

above this named route:

review\_band\_instruments GET /bands/:band\_id/instruments/review  
{:controller=\>“instruments”, :action=\>“review”}

Routes match in the order they are defined. However, the second time you  
call t.resources :instruments with the :collection option you re-define  
the named routes. So you are generating the latter route above but  
matching the former, ending up with the #show action. Take out the first  
route and all will be well.

–  
Josh S.  
[http://blog.hasmanythrough.com](http://blog.hasmanythrough.com)

–  
Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
