# Routing for grouping controllers and avoid modules

**URL:** <https://www.ruby-forum.com/t/routing-for-grouping-controllers-and-avoid-modules/70297>\
**Category:** Rails\
**Created:** [September 28, 2006, 12:15am UTC](https://www.ruby-forum.com/t/routing-for-grouping-controllers-and-avoid-modules/70297 "2006-09-28T00:15:27Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jmcervera](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/51bf81/32.png) [@jmcervera](https://www.ruby-forum.com/u/jmcervera)\
**Post date:** [September 28, 2006, 12:15am UTC](https://www.ruby-forum.com/t/routing-for-grouping-controllers-and-avoid-modules/70297/1 "2006-09-28T00:15:27Z")

</div>

Hello,  
I have this code in my config/routes.rb file

ActionController::Routing::Routes.draw do |map|  
map.supervisor ‘supervisor/:controller/:action/:id’  
map.connect ‘:controller/:action/:id’  
end

I try to group three or four controlles in the same URL path with  
routing, avoinding the use of modules or controller namespaces.  
All the other controllers will have an standard rails URL, matching the  
last route.

It almost works.  
Each incoming path is matched with the appropiated route.  
The problem come with the URL generation functions.  
supervisor\_url(:controller =\> …, :action=\> …) works well, I get the  
expected URL, but url\_for(:controller =\> …, :action=\> …) for all the  
standard controllers fails as is always matched for the first route. All  
the generated urls follow the /supervisor/controller/action path.

How can I make for group only some of the controller in the first route.  
the parameter :requirements =\> {:controller =\> “whatever”} will be a  
solution for only one controller. I think.

Has anybody have an elegant solution (I mean DRY) for this?  
Or I have to do a special rout for each controller?

Thanks

Juanma

---

<div class="post-metadata">

**Author:** ![jmcervera](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/51bf81/32.png) [@jmcervera](https://www.ruby-forum.com/u/jmcervera)\
**Post date:** [September 28, 2006, 12:56am UTC](https://www.ruby-forum.com/t/routing-for-grouping-controllers-and-avoid-modules/70297/2 "2006-09-28T00:56:02Z")

</div>

Why avoid controller modules/namespaces? I use 'em, love 'em, and  
haven’t had any problems with 'em.

Joe

---

<div class="post-metadata">

**Author:** ![jmcervera](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/51bf81/32.png) [@jmcervera](https://www.ruby-forum.com/u/jmcervera)\
**Post date:** [September 28, 2006, 1:13am UTC](https://www.ruby-forum.com/t/routing-for-grouping-controllers-and-avoid-modules/70297/3 "2006-09-28T01:13:00Z")

</div>

Joe R. MUDCRAP-CE wrote:

> Why avoid controller modules/namespaces? I use 'em, love 'em, and  
> haven’t had any problems with 'em.
> 
> Joe

Well, I think that all becomes more messy.  
Besides, I usually follow the advises of people tha knows more than  
I,(Mike C. in this case).

[http://glu.ttono.us/articles/2006/08/30/guide-things-you-shouldnt-be-doing-in-rails](http://glu.ttono.us/articles/2006/08/30/guide-things-you-shouldnt-be-doing-in-rails)

# This is an extract of the article

"Putting Controllers in Namespaces

# This is really more trouble than itâ??s worth. You run into all sorts of crazy errors if you do this and youâ??ll be confused and frustrated. Youâ??ll then ask other people about it and theyâ??ll either blow you off for using namespaces with controllers or procede to get confused and frustrated as well. Then youâ??ll say, â??I with I had listened to Kevin and Chrisâ?. If you want /admin/some\_controller as an URL thatâ??s fine. Use the routing thatâ??s built into rails.

---

<div class="post-metadata">

**Author:** ![jmcervera](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/51bf81/32.png) [@jmcervera](https://www.ruby-forum.com/u/jmcervera)\
**Post date:** [September 28, 2006, 4:00am UTC](https://www.ruby-forum.com/t/routing-for-grouping-controllers-and-avoid-modules/70297/4 "2006-09-28T04:00:35Z")

</div>

Juanma C. Ð¿Ð¸Ñ?ÐµÑ?:

> All the other controllers will have an standard rails URL, matching the  
> last route.

I would do like this :

ActionController::Routing::Routes.draw do |map|  
[‘foo’, ‘bar’, ‘baz’].each do |supervisor\_controller|  
map.connect “/supervisor/#{supervisor\_controller}/:action/:id”,  
:controller =\> supervisor\_controller  
end  
map.connect ‘:controller/:action/:id’  
end

Doing like that you have explicitly stated set of controllers that can  
be mapped to by those urls. + no need to change any other logic or use  
named routes.

Also, I wouldn’t leave CAPTURE\_EM\_ALL route at (the one at the end).

I’m a bit fear of controllers in modules, because you need to use  
link\_to with caution (haven’t you forgot to prefix all your controllers  
with “/” in link\_to params ?)

---

<div class="post-metadata">

**Author:** ![jmcervera](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/51bf81/32.png) [@jmcervera](https://www.ruby-forum.com/u/jmcervera)\
**Post date:** [September 28, 2006, 3:24pm UTC](https://www.ruby-forum.com/t/routing-for-grouping-controllers-and-avoid-modules/70297/5 "2006-09-28T15:24:57Z")

</div>

Or use map.public and public\_url instead of map.connect and url\_for.

---

<div class="post-metadata">

**Author:** ![jmcervera](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/51bf81/32.png) [@jmcervera](https://www.ruby-forum.com/u/jmcervera)\
**Post date:** [September 28, 2006, 1:23am UTC](https://www.ruby-forum.com/t/routing-for-grouping-controllers-and-avoid-modules/70297/6 "2006-09-28T01:23:06Z")

</div>

Juanma C. wrote:

> Joe R. MUDCRAP-CE wrote:
> 
> > Why avoid controller modules/namespaces? I use 'em, love 'em, and  
> > haven’t had any problems with 'em.
> > 
> > Joe
> 
> Well, I think that all becomes more messy.

I think they make things much neater!

> Besides, I usually follow the advises of people tha knows more than  
> I,(Mike C. in this case).

Well, how about DHH then? I think he posted somewhat recently that the  
core team is in no way, shape, or form AGAINST controller  
modules/namespaces.

> [http://glu.ttono.us/articles/2006/08/30/guide-things-you-shouldnt-be-doing-in-rails](http://glu.ttono.us/articles/2006/08/30/guide-things-you-shouldnt-be-doing-in-rails)
> 
> # This is an extract of the article
> 
> "Putting Controllers in Namespaces
> 
> # This is really more trouble than itâ??s worth. You run into all sorts of crazy errors if you do this and youâ??ll be confused and frustrated. Youâ??ll then ask other people about it and theyâ??ll either blow you off for using namespaces with controllers or procede to get confused and frustrated as well. Then youâ??ll say, â??I with I had listened to Kevin and Chrisâ?. If you want /admin/some\_controller as an URL thatâ??s fine. Use the routing thatâ??s built into rails.

I asked them for specifics on problems, but never got a concrete answer.  
I’ll stick to my own experience.

Joe
