# Resource with multiple routes

**URL:** <https://www.ruby-forum.com/t/resource-with-multiple-routes/87944>\
**Category:** Rails\
**Created:** [March 7, 2007, 1:31am UTC](https://www.ruby-forum.com/t/resource-with-multiple-routes/87944 "2007-03-07T01:31:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [March 7, 2007, 1:31am UTC](https://www.ruby-forum.com/t/resource-with-multiple-routes/87944/1 "2007-03-07T01:31:56Z")

</div>

Hello folks.

My application has some resource types which exist at multiple places  
within  
the app. For example, for the discussion model, there are general  
discussions, and discussions specific to a particular group.

Things have worked out pretty well so far. My routes are defined as  
such:

map.resources :discussions do |d|  
d.resources :discussion\_messages  
end

map.resources :groups do |g|  
g.resources :discussions do |d|  
d.resources :discussion\_messages  
end  
end

And it doesn’t take much logic in my controller to make things work out.

However, I’m running into a problem with some of the url generators. For  
example, discussions\_path. Ideally, discussions\_path by itself would  
generate /discussions, and discussions\_path @group would generate  
group/@group/discussions. However, discussions\_path @group results in an  
error.

Is this configuration even supported by rails? Or do I need to bite the  
bullet and make two controllers, etc? Any way I could trick the routing  
and/or url generators into doing what I want?

## Thanks for any pointers.

John Joseph B.  
[http://lyceum.ibiblio.org](http://lyceum.ibiblio.org)  
[http://blog.johnjosephbachir.org](http://blog.johnjosephbachir.org)  
aim/yim/msn/jabber.org/gtalk: johnjosephbachir  
713-494-2704  
[http://jjb.cc](http://jjb.cc)

---

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [March 7, 2007, 2:21am UTC](https://www.ruby-forum.com/t/resource-with-multiple-routes/87944/2 "2007-03-07T02:21:58Z")

</div>

Hi –

On 3/6/07, John Joseph B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> end
> 
> map.resources :groups do |g|  
> g.resources :discussions do |d|  
> d.resources :discussion\_messages  
> end  
> end

When you do d.resources :discussion\_messages, you’re getting a bunch  
of named route methods: discussion\_messages\_url,  
discussion\_message\_url, etc. But you’ve done this twice, so the second  
set clobbers the first set.

In other words, you can’t have two named routes with the same name  
distinguished only by their method signature. There can only be one  
with a given name.

So… you probably want to use the :name\_prefix specifier – something  
along the lines of:

map.resources :groups do |g|  
g.resources :discussions, :name\_prefix =\> “group\_” do |d|  
d.resources :discussion\_messages, :name\_prefix =\> “group\_”  
end  
end

Then you can do:

discussions\_path  
group\_discussions\_path(@group)

and so forth.

David

–  
Q. What is THE Ruby book for Rails developers?  
A. RUBY FOR RAILS by David A. Black ([http://www.manning.com/black](http://www.manning.com/black))  
(See what readers are saying! [http://www.rubypal.com/r4rrevs.pdf](http://www.rubypal.com/r4rrevs.pdf))  
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?  
A. Ruby Power and Light, LLC ([http://www.rubypal.com](http://www.rubypal.com))

---

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [March 7, 2007, 2:28am UTC](https://www.ruby-forum.com/t/resource-with-multiple-routes/87944/3 "2007-03-07T02:28:00Z")

</div>

On Mar 6, 7:20 pm, “David A. Black” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> In other words, you can’t have two named routes with the same name  
> distinguished only by their method signature. There can only be one  
> with a given name.

Thanks so much David, that succinctly explains why I too have been  
having problems with my routes from time to time. Sure wish that was  
made clearer in the Rails docs.

Jeff

---

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [March 7, 2007, 3:35am UTC](https://www.ruby-forum.com/t/resource-with-multiple-routes/87944/4 "2007-03-07T03:35:06Z")

</div>

On Mar 6, 8:20 pm, “David A. Black” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> discussions\_path  
> group\_discussions\_path(@group)

Wow, thanks David! This is exactly the level-of-indirection that I was  
looking for but figured did not exist. It works perfectly.

This inspires two questions:

[1] Where (if anywhere) in the rails documentation is the :name\_prefix  
flag documented? Or any of the routing features, for that matter? Does  
it blow anyone else’s mind that there is only one hit on google for  
“ActionController::Routing::Routes”?

[2] My current solution for differentiating between the two discussion  
realms in my controller is to overwrite  
ApplicationController.default\_template\_name as follows:

def default\_template\_name(action\_name = self.action\_name)  
if action\_name  
action\_name = action\_name.to\_s  
if action\_name.include?(‘/’) && template\_path\_includes\_controller?  
(action\_name)  
action\_name = strip\_out\_controller(action\_name)  
end  
end  
context = @group ? ‘group\_’ : ‘global\_’  
“#{self.class.controller\_path}/#{context}#{action\_name}”  
end

(Everything is the same other than the addition of the penultimate  
line and the modification of the last line).

Is there a better way to do this?

Thanks  
John
