# Routing with namespaces using edge rails

**URL:** <https://www.ruby-forum.com/t/routing-with-namespaces-using-edge-rails/109784>\
**Category:** Rails\
**Created:** [July 19, 2007, 12:47am UTC](https://www.ruby-forum.com/t/routing-with-namespaces-using-edge-rails/109784 "2007-07-19T00:47:51Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![peter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/278dde/32.png) [@peter](https://www.ruby-forum.com/u/peter)\
**Post date:** [July 19, 2007, 12:47am UTC](https://www.ruby-forum.com/t/routing-with-namespaces-using-edge-rails/109784/1 "2007-07-19T00:47:51Z")

</div>

Hi,

I was wondering if anyone came across a similar scenario when working  
with namespaced routes with edge rails.

Consider an application with controllers customers and products that  
also have other resources.

map.resources :customers do |customer|  
customer.resources :notes  
customer.resources :tags  
end

map.resources :products do |product|  
customer.resources :notes  
customer.resources :tags  
end

Now of course this poses a problem as it stands because the notes and  
tags controllers will conflict. I know there are polymorphic routes/  
controllers ([http://revolutiononrails.blogspot.com/2007/05/drying-up-](http://revolutiononrails.blogspot.com/2007/05/drying-up-)  
polymorphic-controllers.html) however this doesnt make sense if the  
controllers have little shared logic/behaviour; only a common name. I  
know I can just use a different name, but with the addition of  
namespaces this should no longer be a concern with a proper design.

So… I’m thinking something like:

/app/controllers/customers/root\_controller.rb  
/app/controllers/customers/tags\_controller.rb  
/app/controllers/customers/notes\_controller.rb  
/app/controllers/products/root\_controller.rb  
/app/controllers/products/tags\_controller.rb  
/app/controllers/products/notes\_controller.rb

with routes like:

map.namespace(:customers) do |customers|  
customers.resources :root, :has\_many =\> [:tags, :notes]  
end  
map.namespace(:products) do |products|  
products.resources :root, :has\_many =\> [:tags, :notes]  
end

creating routes like:  
/customers/root/#{root\_id}  
/customers/root/#{root\_id}/tags/#{id}  
/customers/root/#{root\_id}/notes/#{id  
/products/root/#{root\_id}  
/products/root/#{root\_id}/tags/#{id}  
/products/root/#{root\_id}/notes/#{id

This will work. But we have an unnecessary /root/ in the route. I  
suppose the solution would be adding a flag to the namespace function  
to pass a root controller with a default name of “root\_controller” so  
the root of the namespace can itself be a resource. Can this be done?  
What do you guys think about it as an addition to rails?

This leads me to another scenario. Namespaced models… quite simply  
as it stands you can in /app/models/ make a folder called /customer/  
there add tag.rb and note.rb. With models Customer::Tag and  
Customer::Note. One thing is the table\_name will remain “tags” and  
“notes”, which in this case should be prefixed by the namespace name  
like “customer\_tags” and “customer\_notes”. Also with the ability to  
have a root model like Customer in /customer/root.rb.

I’d love to hear that this can already be done as rails stands. I can  
make the patches and submit them, however I don’t want to divulge from  
other design considerations.

Regards,

Peter
