Most of my clients prefer to have a separate admin section. In turn, I like to have separate controllers for admin section and front-end in my Rails app. This is not as straightforward as it might seem, especially if you like to use scaffolding for admin controller.
The goal is to get 2 separate RESTful controllers, admin & front-end controller, one model and for admin pages to have scaffolding.
Here is the easiest way I found so far to accomplish this. This example generates categories model and controllers for it.
./script/generate controller admin/categories ./script/generate scaffold category name:string
This will generate an empty controller in admin namespace and a scaffolded resource for front-end controller.
Now we have everything generated we just need to make it work with admin controller and not with front-end.
.namespace :admin do |admin| admin.resources :categories end
That’s it. Now you’ll have /admin/categories for all administrative tasks and you have a free controller for front-end actions.
You might wonder why not just generate scaffold for admin/categories… The reason is that you’ll also get a model that is namespaced in admin (i.e. model would be Admin::Category). Scaffolded views also wouldn’t work as it seems that generator doesn’t take into account the fact that you are using a namespace.