繁体   English   中英

如何在没有资源的情况下使用 Rails 路由,仅使用 id

[英]How to use Rails routing without resources, with use only ids

描述。

我想在我的网站上过滤产品,但我不会使用嵌套资源。

过滤有4级。

例如,

https://example.com/london/car/bmw/red

或者

https://example.com/:city/:category/:brand/:color

路线.rb

get '/:one_id', to: 'categories#show', as: 'category_one'
get '/:one_id/:two_id', to: 'categories#show', as: 'category_two'
get '/:one_id/:two_id/:three_id', to: 'categories#show', as: 'category_three'
get '/:one_id/:two_id/:three_id/:four_id', to: 'categories#show', as: 'category_four'

为什么我使用名称 id 像 ":one_id"、":two_id" 等?

因为“:one_id”可能包含多个对象(过滤器)。

例如,

https://example.com/london/

https://example.com/bmw/

https://example.com/woman/

即,“:one_id”可以是城市、类别或性别(多个实体),“:two_id”可以是类别、品牌或颜色等。

问题:

组织此类路由的最佳实践是什么?

配置:

Ruby 2.5.1

导轨 5.2.2.1

您可以使用shallow来避免在 rails 中深度嵌套资源。 您可以阅读本教程以获取更多信息。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM