# Namespaced controller question

**URL:** <https://www.ruby-forum.com/t/namespaced-controller-question/184710>\
**Category:** Rails\
**Created:** [March 7, 2010, 4:57am UTC](https://www.ruby-forum.com/t/namespaced-controller-question/184710 "2010-03-07T04:57:56Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![pkoebbe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ec9cab/32.png) [@pkoebbe](https://www.ruby-forum.com/u/pkoebbe)\
**Post date:** [March 7, 2010, 4:57am UTC](https://www.ruby-forum.com/t/namespaced-controller-question/184710/1 "2010-03-07T04:57:56Z")

</div>

I have a utility controller that has an action in it. Before I changed  
to namespaced controllers, I could access the action with:

/utility/my\_action

There are no resources associated with the controller, so I’m assuming  
the stock route

map.connect ‘:controller/:action/:id’

picked it up. However, for reasons I don’t want to go into detail about,  
I moved the utility controller into a namespace (Web), and the route  
started failing. Ultimately, I added

map.connect ‘web/utility/:action’, :controller =\> ‘web/utility’

to routes.rb and it started working again. I do not understand why I had  
to specify ‘web/utility’ in the map.connect statement. Why didn’t the  
stock map.connect pick it up? And why wouldn’t

map.connect ‘web/:controller/:action’

work?

Thanks,  
Phillip

---

<div class="post-metadata">

**Author:** ![pkoebbe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ec9cab/32.png) [@pkoebbe](https://www.ruby-forum.com/u/pkoebbe)\
**Post date:** [March 7, 2010, 6:25am UTC](https://www.ruby-forum.com/t/namespaced-controller-question/184710/2 "2010-03-07T06:25:24Z")

</div>

On Sun, 2010-03-07 at 04:57 +0100, Phillip K. wrote:

> picked it up. However, for reasons I don’t want to go into detail about,
> 
> work?

* * *

assuming that the first line of  
app/controllers/web/utility\_controller.rb looks like

class Web::UtilityController \< ApplicationController

I don’t think you really need to do a whole lot with routes.rb at all  
(the views would necessarily have to follow a similar pathing in  
app/views/web/utility) but I also wonder whether you are using the  
plural utilities instead of the singular utility, etc.

Craig

–  
This message has been scanned for viruses and  
dangerous content by MailScanner, and is  
believed to be clean.

---

<div class="post-metadata">

**Author:** ![pkoebbe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ec9cab/32.png) [@pkoebbe](https://www.ruby-forum.com/u/pkoebbe)\
**Post date:** [March 7, 2010, 1:10pm UTC](https://www.ruby-forum.com/t/namespaced-controller-question/184710/3 "2010-03-07T13:10:13Z")

</div>

Craig W. wrote:

> On Sun, 2010-03-07 at 04:57 +0100, Phillip K. wrote:
> 
> > picked it up. However, for reasons I don’t want to go into detail about,
> > 
> > work?
> 
> * * *
> 
> assuming that the first line of  
> app/controllers/web/utility\_controller.rb looks like
> 
> class Web::UtilityController \< ApplicationController

No, it’s actually

class Web::UtilityController \< Web::BaseController

where

class Web::BaseController \< ApplicationController

> I don’t think you really need to do a whole lot with routes.rb at all

I didn’t think so either. That’s why I was surprised when the default  
route of

map.connect ':controller/:action/:id

didn’t work. But since I’m not passing an id to this action, I even  
tried

map.connect ':controller/:action

but that didn’t work either.

> (the views would necessarily have to follow a similar pathing in  
> app/views/web/utility)

Right. I got all that taken care of. I’ve had success in the past using  
namespaced controllers, but this is the first time I have done so with  
one that wasn’t a resource. I’ve never had any difficulty when I have  
done this something like this in routes:

map.namespace :web do |web|  
web.resources :pages  
end

I don’t really want to resource this utility controller as it is just  
for actions that do little odds and ends that need to be done, but don’t  
really belong anywhere else. The particular case in question is storing  
the current state of an expandable menu structure. As the user navigates  
from page to page, I restore the menu to the “current” state so the user  
doesn’t have to keep expanding things over and over. It’s stored in the  
session, not the database, so I didn’t want to put it on the People  
(user) controller.

> but I also wonder whether you are using the  
> plural utilities instead of the singular utility, etc.

No, just singular. Since it’s not a resource, it makes more sense to me  
to have it singular. Much like HomeController.

Thanks for your input!

Peace.

---

<div class="post-metadata">

**Author:** ![pkoebbe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ec9cab/32.png) [@pkoebbe](https://www.ruby-forum.com/u/pkoebbe)\
**Post date:** [March 7, 2010, 6:21pm UTC](https://www.ruby-forum.com/t/namespaced-controller-question/184710/4 "2010-03-07T18:21:45Z")

</div>

On Mar 7, 3:57 am, Phillip K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> picked it up. However, for reasons I don’t want to go into detail about,  
> I moved the utility controller into a namespace (Web), and the route  
> started failing. Ultimately, I added

The only thing I’ve noticed with namespaced controllers like that and  
the :controller/:action/:id default route is that a restart seems to  
be required to pick up new controllers.

Fred

---

<div class="post-metadata">

**Author:** ![pkoebbe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ec9cab/32.png) [@pkoebbe](https://www.ruby-forum.com/u/pkoebbe)\
**Post date:** [March 7, 2010, 8:59pm UTC](https://www.ruby-forum.com/t/namespaced-controller-question/184710/5 "2010-03-07T20:59:12Z")

</div>

Frederick C. wrote:

> On Mar 7, 3:57ï¿½am, Phillip K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > picked it up. However, for reasons I don’t want to go into detail about,  
> > I moved the utility controller into a namespace (Web), and the route  
> > started failing. Ultimately, I added
> 
> The only thing I’ve noticed with namespaced controllers like that and  
> the :controller/:action/:id default route is that a restart seems to  
> be required to pick up new controllers.
> 
> Fred

Unfortunately, there were many restarts, and not a single one of them  
seemed to help. 🙂

Oh well. It is working. I just wanted to try to understand why it didn’t  
work the other way.

Peace.
