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.
- 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.
- 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
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
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
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 “http://domain.com/admin” will get routed to "
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
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
protect those kinds of methods anyhow. But the named route benefits are
Anyhow, I hoped that helped.