简体   繁体   中英

How to use Rails routing without resources, with use only ids

Description.

I want filtering product on my site, but I won't use nested resources.

Filtering has 4 level.

For example,

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

or

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

routes.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'

Why I use names id like ":one_id", ":two_id" etc.?

Because ": one_id" may contain multiple objects (filters).

For example,

https://example.com/london/

https://example.com/bmw/

https://example.com/woman/

ie, ":one_id" may be city, category or sex (multiple entity) and ":two_id" may be category, brand or color, etc.

Question:

What are the best practices for organizing such routing?

Configuration:

Ruby 2.5.1

Rails 5.2.2.1

You can use shallow to avoid deep nesting resources in rails. You can read this tutorial to have more information.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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