rails Controller Namespaces and Routing


You may wish to organize groups of controllers under a namespace. Most commonly, you might group a number of administrative controllers under an Admin:: namespace. You would place these controllers under the app/controllers/admin directory, and you can group them together in your router:

namespace :admin do
   resources :posts , :comments
end

This will create a number of routes for each of the posts and comments controller. ForAdmin::PostsController, Rails will create:

HTTP Verb Path action named helper
GET /admin/posts index admin_posts_path
GET /admin/posts/new new new_admin_post_path
POST /admin/posts create admin_posts_path
GET /admin/posts/:id show admin_post_path(:id)
GET /admin/posts/:id/edit edit edit_admin_post_path(:id)
PUT /admin/posts/:id update admin_post_path(:id)
DELETE /admin/posts/:id destroy admin_post_path(:id)

If you want to route /posts (without the prefix /admin) to Admin::PostsController, you could use

scope :module => "admin" do
   resources :posts , :comments
end

or, for a single case

resources :posts , :module => "admin"

If you want to route /admin/posts to PostsController (without the Admin:: module prefix), you could use

scope "/admin" do
   resources :posts , :comments
end

or, for a single case

resources :posts , :path => "/admin/posts"

In each of these cases, the named routes remain the same as if you did not use scope. In the last case, the following paths map to PostsController:

HTTP Verb Path action named helper
GET /admin/posts index posts_path
GET /admin/posts/new new new_post_path
POST /admin/posts create posts_path
GET /admin/posts/:id show post_path(:id)
GET /admin/posts/:id/edit edit edit_post_path(:id)
PUT /admin/posts/:id update post_path(:id)
DELETE /admin/posts/:id destroy post_path(:id)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值