How to have many controllers under one admin page?


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,…)

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

Thanks by advance

Best regards,


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
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
    controller in the subdirectory [app/controllers/admin]. It also puts it
    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
    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
    instead of adding it manually to each controller. It also can serve as a
    holding place for common code. For example, in

def go_home
redirect to :action => :index

That’s something you type a lot. As a function in
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::
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.resources :whatevers, :controller => “admin/whatevers”

The benefits are more obvious when there’s a handful of controllers,
A heads ups though… Named routes will obviously have admin_ prefixed
them. This creates routes like admin_whatevers_url and the kinda oddly
admin_new_whatever_url instead of the more English
Just remember that the name_prefix truly is a prefix.
3a. I usually also define a method “index” on Admin::CommonController
redirects to the actual controller you want as a default admin
for logins. That way “” will get routed to "". 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
sorts. Do whatever there. You’re the boss. But usually [because I’m
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
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…

I’m a big believer in the named routes and not using the
:controller/:action/:id default, which can inadvertently expose methods
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.


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!


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??


Best regards


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

class Admin::CategoriesController < Admin::CommonController # in

and admin/common does descend from application

class Admin::CommonController < ApplicationController



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

Thanks again!


This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs