This is by far the most popular structure used by any web application.
To have ‘admin’ in sub-folder using to manage the administrative information.
Such as we have ‘category’ model, and we want to manage the scaffold in www.myrailsapp.com/admin/categories instead of www.myrailsapp.com/categories
Too bad, the generators provided by RoR didn’t included this.
Here are the steps you need to do this practices.
- Generate a blank controller named ‘admin/categories’. (mentioned that it is ‘categories’, you have to manually ‘s’ it.)
- Generate a scaffold ‘category’, the model ‘category’ will be generated while the ‘categories_controller’ and related views will be generated too.
- Move all functions from ‘categories_controller’ to ‘admin/categories_controller’.
- Delete ‘categories_controller’.
- Move all views from ‘views/categories’ to ‘views/admin/categories’.
- Delete the folder ‘views/categories’.
- Edit the routes.rb and add this line :
namespace :admin do resources :categories end
- Edit the ‘admin/categories_controller’, modify the redirect_to to specify the namespace being used like this :
redirect_to([:admin,@category], ...)
- Edit the ‘admin/categories_controller’, modify the categories_url to admin_categories_url.
- Now edit all views, do the same with link_to, if you found @categoy, just add :admin to specify the namespace.
- Do the same in all views, change xxx_categories_path to xxx_admin_categories_path.
- Then modify the form view, change from form_for(@category) to form_for([:admin,@category]).
It will works now.
Also, if the Destroy action didn’t work, didn’t popup a confirm message, please include the bundled ‘prototype.js’ and ‘rails.js’ into your template too.
Hope this help.