# How to have many controllers under one admin page?

**URL:** <https://www.ruby-forum.com/t/how-to-have-many-controllers-under-one-admin-page/83597>\
**Category:** Rails\
**Created:** [January 27, 2007, 12:17pm UTC](https://www.ruby-forum.com/t/how-to-have-many-controllers-under-one-admin-page/83597 "2007-01-27T12:17:16Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Matthieu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/da6949/32.png) [@Matthieu](https://www.ruby-forum.com/u/Matthieu)\
**Post date:** [January 27, 2007, 12:17pm UTC](https://www.ruby-forum.com/t/how-to-have-many-controllers-under-one-admin-page/83597/1 "2007-01-27T12:17:16Z")

</div>

Hello,

Since I have many controllers that require administration treatment,  
with the scaffold command I’m able to create one admin page for each of  
these controllers.

How would it be possible to gather all of them under one page?

Or if it’s not possible, how can I have a main Admin page with  
subfolders in which I’ll put every admin interfaces? (like  
[site.com/admin/controller1](http://site.com/admin/controller1), [site.com/admin/controller2](http://site.com/admin/controller2)…)

I’m a total beginner with Ruby on Rails so please try not to skip the  
basic steps 😉

Thanks by advance

Best regards,

Matthieu

---

<div class="post-metadata">

**Author:** ![Matthieu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/da6949/32.png) [@Matthieu](https://www.ruby-forum.com/u/Matthieu)\
**Post date:** [January 27, 2007, 2:36pm UTC](https://www.ruby-forum.com/t/how-to-have-many-controllers-under-one-admin-page/83597/2 "2007-01-27T14:36:40Z")

</div>

While many people dislike the concept of putting admin controllers into  
their own namespace, I don’t. So I’ll let you in on how it’s done along  
with  
some handy routing tricks I’ve picked up along the way.

1. You’ll need to create the controller with script/generate controller  
Admin/Whatevers [where Whatever is the controller name.] which creates  
the  
controller in the subdirectory [app/controllers/admin]. It also puts it  
into  
the namespace Admin, the fullname being Admin::WhateverController.
2. I find it’s easiest to go ahead and create another controller  
Admin::CommonController [created by script/generate controller  
Admin/Common]  
and have all the Admin controllers descend from it. That way you can  
have a  
common layout defined once in Admin::CommonController and it trickles  
down  
instead of adding it manually to each controller. It also can serve as a  
holding place for common code. For example, in  
Admin::CommonController…

def go\_home  
redirect to :action =\> :index  
end

That’s something you type a lot. As a function in  
Admin::CommonController,  
now all the descendant controllers have access to it and you can write  
“go\_home and return” instead of “redirect\_to :action =\> :index” over and  
over. I find this to be one of the biggest benefits to using an Admin::  
namespace.  
3. Anyhow, now about routing. If you’re using REST-y routing then  
there’s a  
super awesome trick to keep the named routes for admin controllers from  
conflicting down the road with non-admin controllers as well.

map.with options :name\_prefix =\> “admin\_”, :path\_prefix =\> “admin/” do  
|m|  
m.resources :whatevers, :controller =\> “admin/whatevers”  
end

The benefits are more obvious when there’s a handful of controllers,  
natch!  
A heads ups though… Named routes will obviously have admin\_ prefixed  
to  
them. This creates routes like admin\_whatevers\_url and the kinda oddly  
named  
admin\_new\_whatever\_url instead of the more English  
new\_admin\_whatever\_url.  
Just remember that the name\_prefix truly is a prefix.  
3a. I usually also define a method “index” on Admin::CommonController  
that  
redirects to the actual controller you want as a default admin  
controller  
for logins. That way “[http://domain.com/admin](http://domain.com/admin)” will get routed to "  
[http://domain.com/admin/whatevers](http://domain.com/admin/whatevers)". You will have to add a route  
[map.admin"/admin", :controller =\> “admin/common”, :action =\> “index”]  
for it as well.  
Of course, you don’t have to redirect. You could use it as a dashboard  
of  
sorts. Do whatever there. You’re the boss. But usually [because I’m  
trying  
to embrace the CRUD and REST] I’m redirected to another controller.  
3b, If you’re not all RESTed up [and why _not_??], you can still use  
that  
map.with\_options to DRY some of the routing.

map.with\_options :controller =\> “admin/whatevers” do |m|  
m.admin\_whatevers “admin/whatevers”, :action =\> “index”  
m.admin\_new\_whatever “admin/whatevers/new”, :action =\> “new”  
# And so on…  
end

I’m a big believer in the named routes and not using the  
:controller/:action/:id default, which can inadvertently expose methods  
you  
might not want exposed all the time. Though you should be using  
“private” to  
protect those kinds of methods anyhow. But the named route benefits are  
reason enough.

Anyhow, I hoped that helped.

RSL

---

<div class="post-metadata">

**Author:** ![Matthieu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/da6949/32.png) [@Matthieu](https://www.ruby-forum.com/u/Matthieu)\
**Post date:** [January 27, 2007, 3:03pm UTC](https://www.ruby-forum.com/t/how-to-have-many-controllers-under-one-admin-page/83597/3 "2007-01-27T15:03:33Z")

</div>

Russell N. wrote:

> A very helpul solution!

Thank you a lot for your detailed explanations!

For the first two parts, I think I’ll be able to manage it.  
For the third, I don’t really know what is REST or CRUD, so I’ll read  
some stuff about it before trying it.

And again, thanks a lot!

Matthieu

---

<div class="post-metadata">

**Author:** ![Matthieu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/da6949/32.png) [@Matthieu](https://www.ruby-forum.com/u/Matthieu)\
**Post date:** [January 27, 2007, 9:04pm UTC](https://www.ruby-forum.com/t/how-to-have-many-controllers-under-one-admin-page/83597/4 "2007-01-27T21:04:37Z")

</div>

Hi again!

When I try to follow what you told me ,I get this message:

NameError in Admin/commonController#index

uninitialized constant Admin::CommonController

RAILS\_ROOT: ./script/…/config/…

Do you have any idea of how to avoid this??

Thanks!!

Best regards

Matthieu

---

<div class="post-metadata">

**Author:** ![Matthieu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/da6949/32.png) [@Matthieu](https://www.ruby-forum.com/u/Matthieu)\
**Post date:** [January 28, 2007, 5:01pm UTC](https://www.ruby-forum.com/t/how-to-have-many-controllers-under-one-admin-page/83597/5 "2007-01-28T17:01:57Z")

</div>

Just making sure I pointed out that [for example]  
Admin::CategoriesController needs to descend from  
Admin::CommonController  
and not ApplicationController

class Admin::CategoriesController \< Admin::CommonController # in  
admin/categories

and admin/common _does_ descend from application

class Admin::CommonController \< ApplicationController

RSL

---

<div class="post-metadata">

**Author:** ![Matthieu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/da6949/32.png) [@Matthieu](https://www.ruby-forum.com/u/Matthieu)\
**Post date:** [January 28, 2007, 2:27pm UTC](https://www.ruby-forum.com/t/how-to-have-many-controllers-under-one-admin-page/83597/6 "2007-01-28T14:27:42Z")

</div>

Hello,

I somehow managed to make it works, so ignore the previous message!!

Thanks again!

Matthieu
