Stv*_*vyu 5 rest routes ruby-on-rails nested-routes
我刚刚开始学习Ruby on Rails并在一个简单的站点上工作,该站点具有以下设置:
resources :categories do
resources :products
end
resources :products do
resources :features
end
Run Code Online (Sandbox Code Playgroud)
但是我不想暴露网址 products_controller
/products(.:format) products#index
/products(.:format) products#create
/products/new(.:format) products#new
/products/:id/edit(.:format) products#edit
/products/:id(.:format) products#show
/products/:id(.:format) products#update
/products/:id(.:format) products#update
/products/:id(.:format) products#destroy
Run Code Online (Sandbox Code Playgroud)
我只需要看起来如下的路线
/products/:product_id/features(.:format) features#index
/products/:product_id/features(.:format) features#create
/products/:product_id/features/new(.:format) features#new
/features/:id/edit(.:format) features#edit
/features/:id(.:format) features#show
/features/:id(.:format) features#update
/features/:id(.:format) features#update
/features/:id(.:format) features#destroy
Run Code Online (Sandbox Code Playgroud)
我知道上面的路由可以通过标记来完成shallow: true,但它仍然会暴露到products_controller的restful路径,是否还有这个呢?
j-d*_*exx 13
您可以使用only或except将其限制为所需的操作.仅使用空数组应删除路径.
resources :categories do
resources :products
end
resources :products, only: [] do
resources :features
end
Run Code Online (Sandbox Code Playgroud)
所以现在如果我耙路线
category_products GET /categories/:category_id/products(.:format) products#index
POST /categories/:category_id/products(.:format) products#create
new_category_product GET /categories/:category_id/products/new(.:format) products#new
edit_category_product GET /categories/:category_id/products/:id/edit(.:format) products#edit
category_product GET /categories/:category_id/products/:id(.:format) products#show
PATCH /categories/:category_id/products/:id(.:format) products#update
PUT /categories/:category_id/products/:id(.:format) products#update
DELETE /categories/:category_id/products/:id(.:format) products#destroy
categories GET /categories(.:format) categories#index
POST /categories(.:format) categories#create
new_category GET /categories/new(.:format) categories#new
edit_category GET /categories/:id/edit(.:format) categories#edit
category GET /categories/:id(.:format) categories#show
PATCH /categories/:id(.:format) categories#update
PUT /categories/:id(.:format) categories#update
DELETE /categories/:id(.:format) categories#destroy
product_features GET /products/:product_id/features(.:format) features#index
POST /products/:product_id/features(.:format) features#create
new_product_feature GET /products/:product_id/features/new(.:format) features#new
edit_product_feature GET /products/:product_id/features/:id/edit(.:format) features#edit
product_feature GET /products/:product_id/features/:id(.:format) features#show
PATCH /products/:product_id/features/:id(.:format) features#update
PUT /products/:product_id/features/:id(.:format) features#update
DELETE /products/:product_id/features/:id(.:format) features#destroy
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1737 次 |
| 最近记录: |