Rails-多态嵌套路线

时间:2018-07-25 17:30:20

标签: ruby-on-rails routes polymorphism ruby-on-rails-5 friendly-url

我有一个多态模型“项目”设置,因此它可以属于细分,部门或地点。

我要实现的路线是:

/category
/category/place
/category/place/item **
/category/place/division
/category/place/division/item **
/category/place/division/subdivision
/category/place/division/subdivision/item **

**项目可以属于一个地点,部门或细分。永远不会超过一个。我正在使用Friendly_ID创建这些条形网址。

我的routes.rb文件当前如下所示:

resources :categories, path: ''
get ':category/:place', to: 'places#show', as: :place
get ':category/:place/:item', to: 'items#show', as: :item
get ':category/:place/:division', to: 'divisions#show', as: :division
get ':category/:place/:division/:subdivision', to: 'subdivisions#show', as: :subdivision

目前,我的路线中只能设置一个“项目”。如何通过位置,分区或细分来访问“项目”?

1 个答案:

答案 0 :(得分:0)

您可以将所有这些路线都指向items#show,即

get ':category/:place/:item', to: 'items#show', as: :place_item
get ':category/:place/:division/:item', to: 'items#show', as: :division_item
get ':category/:place/:division/:subdivision/:item', to: 'items#show', as: :subdivision_item