Ruby on rails 过度使用路由自定义-自定义\u计数(Rails 5)
我正在使用Ruby on rails 过度使用路由自定义-自定义\u计数(Rails 5),ruby-on-rails,routing,ruby-on-rails-5,Ruby On Rails,Routing,Ruby On Rails 5,我正在使用rails\u最佳实践gem,它告诉我有一个错误: 过度使用路线自定义(自定义数量>8) 导致以下路径: api_stores_path GET /stores/api(.:format) stores#api printer_store_path GET /stores/:id/printer(.:format) stores#printer delete_store_path GET /stores/:id/delete(.:format) stores#delete inve
rails\u最佳实践gem,它告诉我有一个错误:
过度使用路线自定义(自定义数量>8)
导致以下路径:
api_stores_path GET /stores/api(.:format) stores#api
printer_store_path GET /stores/:id/printer(.:format) stores#printer
delete_store_path GET /stores/:id/delete(.:format) stores#delete
inventory_store_path GET /stores/:id/inventory(.:format) stores#inventory
daysheet_store_path GET /stores/:id/daysheet(.:format) stores#daysheet
detailed_daysheet_store_path GET /stores/:id/detailed_daysheet(.:format) stores#detailed_daysheet
labels_store_path GET /stores/:id/labels(.:format) stores#labels
DELETE /stores/:id/inventory(.:format) stores#inventory
restore_store_path PATCH /stores/:id/restore(.:format) stores#restore
print_labels_store_path PATCH /stores/:id/print_labels(.:format) stores#print_labels
POST /stores/:id/daysheet(.:format) stores#daysheet
重构后,我需要它仍能像现在一样使用get
路由,如/stores/7/inventory
和/stores/18/printer
如何压缩这些get路由以实现相同的路由目标?一种方法是:
resources :stores do
scope module: :stores do
resource :printer, only: [:show]
resource :daysheet, only: [:show, :create]
resource :detailed_daysheet, only: [:show]
resource :inventory, only: [:show, :destroy]
resources :labels, only: [:index]
resources :print_labels, only: [:update]
resource :restore, only: [:update]
end
collection do
get :api
end
member do
get :delete
end
end
这给了你:
store_printer GET /stores/:store_id/printer(.:format) stores/printers#show
store_daysheet GET /stores/:store_id/daysheet(.:format) stores/daysheets#show
POST /stores/:store_id/daysheet(.:format) stores/daysheets#create
store_detailed_daysheet GET /stores/:store_id/detailed_daysheet(.:format) stores/detailed_daysheets#show
store_inventory GET /stores/:store_id/inventory(.:format) stores/inventories#show
DELETE /stores/:store_id/inventory(.:format) stores/inventories#destroy
store_labels GET /stores/:store_id/labels(.:format) stores/labels#index
store_print_label PATCH /stores/:store_id/print_labels/:id(.:format) stores/print_labels#update
PUT /stores/:store_id/print_labels/:id(.:format) stores/print_labels#update
store_restore PATCH /stores/:store_id/restore(.:format) stores/restores#update
PUT /stores/:store_id/restore(.:format) stores/restores#update
api_stores GET /stores/api(.:format) stores#api
delete_store GET /stores/:id/delete(.:format) stores#delete
stores GET /stores(.:format) stores#index
POST /stores(.:format) stores#create
new_store GET /stores/new(.:format) stores#new
edit_store GET /stores/:id/edit(.:format) stores#edit
store GET /stores/:id(.:format) stores#show
PATCH /stores/:id(.:format) stores#update
PUT /stores/:id(.:format) stores#update
DELETE /stores/:id(.:format) stores#destroy
当然,这需要创建许多新的嵌套控制器,例如Stores::Printers
,它们将驻留在app/controllers/Stores/Printers\u controller.rb
中。但是,您现在使用的是标准的RESTful路线,我想有些人认为这是一件好事
此外,对于嵌套路由,您将在参数中使用:store_id
,而不是id
collection api
和member delete
看起来仍然很奇怪,但我不确定其意图是什么。一种方法是:
resources :stores do
scope module: :stores do
resource :printer, only: [:show]
resource :daysheet, only: [:show, :create]
resource :detailed_daysheet, only: [:show]
resource :inventory, only: [:show, :destroy]
resources :labels, only: [:index]
resources :print_labels, only: [:update]
resource :restore, only: [:update]
end
collection do
get :api
end
member do
get :delete
end
end
这给了你:
store_printer GET /stores/:store_id/printer(.:format) stores/printers#show
store_daysheet GET /stores/:store_id/daysheet(.:format) stores/daysheets#show
POST /stores/:store_id/daysheet(.:format) stores/daysheets#create
store_detailed_daysheet GET /stores/:store_id/detailed_daysheet(.:format) stores/detailed_daysheets#show
store_inventory GET /stores/:store_id/inventory(.:format) stores/inventories#show
DELETE /stores/:store_id/inventory(.:format) stores/inventories#destroy
store_labels GET /stores/:store_id/labels(.:format) stores/labels#index
store_print_label PATCH /stores/:store_id/print_labels/:id(.:format) stores/print_labels#update
PUT /stores/:store_id/print_labels/:id(.:format) stores/print_labels#update
store_restore PATCH /stores/:store_id/restore(.:format) stores/restores#update
PUT /stores/:store_id/restore(.:format) stores/restores#update
api_stores GET /stores/api(.:format) stores#api
delete_store GET /stores/:id/delete(.:format) stores#delete
stores GET /stores(.:format) stores#index
POST /stores(.:format) stores#create
new_store GET /stores/new(.:format) stores#new
edit_store GET /stores/:id/edit(.:format) stores#edit
store GET /stores/:id(.:format) stores#show
PATCH /stores/:id(.:format) stores#update
PUT /stores/:id(.:format) stores#update
DELETE /stores/:id(.:format) stores#destroy
当然,这需要创建许多新的嵌套控制器,例如Stores::Printers
,它们将驻留在app/controllers/Stores/Printers\u controller.rb
中。但是,您现在使用的是标准的RESTful路线,我想有些人认为这是一件好事
此外,对于嵌套路由,您将在参数中使用:store_id
,而不是id
collection-api
和member-delete
看起来仍然很奇怪,但我不确定其意图是什么。可能使用了嵌套资源。你试过什么?@jvillian我试过get':store_action',约束:{store_action:/printer | delete | inventory | daysheet | detailed | daysheet | labels/}
在成员do
中,但这导致了一个错误。我如何使用这里的嵌套资源?我在网上搜索了很长一段时间都没有成功。你读过这篇吗?可能是使用嵌套资源。你试过什么?@jvillian我试过get':store_action',约束:{store_action:/printer | delete | inventory | daysheet | detailed | daysheet | labels/}
在成员do
中,但这导致了一个错误。我如何使用这里的嵌套资源?我已经在网上搜索了很长一段时间,但没有成功。你读过这篇文章吗?嗯,不可否认,我发现这种方法很难理解,但它确实符合我的要求。我只是有点犹豫是否给它打勾,因为它需要创建几个新的资源,而且路径不一样(这意味着我需要重构并冒着破坏站点中多个链接的风险)。我真的很感激你在这方面所做的工作,如果我没有看到更好的答案,我会很高兴把你的答案标记为正确的。谢谢你是说路径名不一样吗?如果这是一个问题,您可以使用as:
参数赚钱。(我会更新代码以进行演示,但如果这对您来说是错误的,则没有太多理由。)我认为唯一不同的途径可能是打印标签。还有其他的吗?虽然我通常真的很喜欢rails_best_practices gem,但我认为这是一个案例,它将我推向一个比我已经使用的更复杂的解决方案。谢谢你为回答这个问题所做的一切工作,但就我而言,我会坚持我的解决方案。希望这个帖子将来能帮助其他人。谢谢诚然,我发现这种方法很难理解,但它确实符合我的要求。我只是有点犹豫是否给它打勾,因为它需要创建几个新的资源,而且路径不一样(这意味着我需要重构并冒着破坏站点中多个链接的风险)。我真的很感激你在这方面所做的工作,如果我没有看到更好的答案,我会很高兴把你的答案标记为正确的。谢谢你是说路径名不一样吗?如果这是一个问题,您可以使用as:
参数赚钱。(我会更新代码以进行演示,但如果这对您来说是错误的,则没有太多理由。)我认为唯一不同的途径可能是打印标签。还有其他的吗?虽然我通常真的很喜欢rails_best_practices gem,但我认为这是一个案例,它将我推向一个比我已经使用的更复杂的解决方案。谢谢你为回答这个问题所做的一切工作,但就我而言,我会坚持我的解决方案。希望这个帖子将来能帮助其他人。谢谢