簡體   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