在Rails中可以通过namespace来管理controller。
首先生成scaffold:
script/generate scaffold article title:string body:text
在routes.rb中添加如下路由:
map.namespace :admin do |admin|
admin.resources :articles
end
由意味着给article增加了一个名为admin的namespace,将controllers中的articles_controller.rb修改为controllers/admin/articles_controller.rb,也就是将这个控制器放到admin这个文件夹中。
先看一下相应的路由规则,执行rake roures:
admin_articles GET /admin/articles {:action=>"index", :controller=>"admin/articles"}
formatted_admin_articles GET /admin/articles.:format {:action=>"index", :controller=>"admin/articles"}
POST /admin/articles {:action=>"create", :controller=>"admin/articles"}
POST /admin/articles.:format {:action=>"create", :controller=>"admin/articles"}
new_admin_article GET /admin/articles/new {:action=>"new", :controller=>"admin/articles"}
formatted_new_admin_article GET /admin/articles/new.:format {:action=>"new", :controller=>"admin/articles"}
edit_admin_article GET /admin/articles/:id/edit {:action=>"edit", :controller=>"admin/articles"}
formatted_edit_admin_article GET /admin/articles/:id/edit.:format {:action=>"edit", :controller=>"admin/articles"}
admin_article GET /admin/articles/:id {:action=>"show", :controller=>"admin/articles"}
formatted_admin_article GET /admin/articles/:id.:format {:action=>"show", :controller=>"admin/articles"}
PUT /admin/articles/:id {:action=>"update", :controller=>"admin/articles"}
PUT /admin/articles/:id.:format {:action=>"update", :controller=>"admin/articles"}
DELETE /admin/articles/:id {:action=>"destroy", :controller=>"admin/articles"}
DELETE /admin/articles/:id.:format {:action=>"destroy", :controller=>"admin/articles"}
/:controller/:action/:id
/:controller/:action/:id.:format
对比一下不加admin这个namespace时的路由:
article_index GET /article {:action=>"index", :controller=>"article"}
formatted_article_index GET /article.:format {:action=>"index", :controller=>"article"}
POST /article {:action=>"create", :controller=>"article"}
POST /article.:format {:action=>"create", :controller=>"article"}
new_article GET /article/new {:action=>"new", :controller=>"article"}
formatted_new_article GET /article/new.:format {:action=>"new", :controller=>"article"}
edit_article GET /article/:id/edit {:action=>"edit", :controller=>"article"}
formatted_edit_article GET /article/:id/edit.:format {:action=>"edit", :controller=>"article"}
article GET /article/:id {:action=>"show", :controller=>"article"}
formatted_article GET /article/:id.:format {:action=>"show", :controller=>"article"}
PUT /article/:id {:action=>"update", :controller=>"article"}
PUT /article/:id.:format {:action=>"update", :controller=>"article"}
DELETE /article/:id {:action=>"destroy", :controller=>"article"}
DELETE /article/:id.:format {:action=>"destroy", :controller=>"article"}
/:controller/:action/:id
/:controller/:action/:id.:format
所有的路由规则上面,都加上了admin这个namespace。
下面就要将views目录下面的articles目录改到views/admin目录下面,因为根据路由规则,会到views/admin/articles/目录下面去查找相应的模板。
另外,还需要将原来的new_article_url | new_article_path等helper方法修改为new_admin_article_url | new_admin_article_path,将这些helper方法全部加上admin这个namespace。