gpt4 book ai didi

ruby-on-rails - Rails 4嵌套了资源,但没有公开父级的RESTful路由?

转载 作者:行者123 更新时间:2023-12-04 16:52:35 24 4
gpt4 key购买 nike

我刚刚开始学习Ruby on Rails,并在一个具有以下设置的简单网站上工作:

  resources :categories do
resources :products
end

resources :products do
resources :features
end

但是我不想将URL公开给 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

我只需要如下所示的路线
/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

我知道上面的路由可以通过标记 shallow: true来完成,但是它仍然会向products_controller公开 Restful 路径,这附近是否存在?

最佳答案

您可以通过仅使用或除外将其限制为所需的操作。仅与空数组一起使用应删除路由。

  resources :categories do
resources :products
end

resources :products, only: [] do
resources :features
end

所以现在如果我耙路线
 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

关于ruby-on-rails - Rails 4嵌套了资源,但没有公开父级的RESTful路由?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27425392/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com