【问题标题】:Rails - Self joins with ancestry, possible to have nested routes and slugs?Rails - 与祖先的自我连接,可能有嵌套的路线和蛞蝓?
【发布时间】:2018-07-08 23:17:10
【问题描述】:

目标是创建以下 URL 路由:

localhost:3000/category-1/location-1/

localhost:3000/category-1/location-1/sub-location-2

localhost:3000/category-1/location-1/sub-location-2/sub-location-3

我有一系列已划分为类别的位置。每个位置可以属于 1 个类别,也可以属于 1 个位置,或者是一个根位置。

Category.rb

has_many :locations

Location.rb

belongs_to :category
has_ancestry

我曾考虑将我的 Location 模型拆分为 Location 和 SubLocation,但它不够灵活,无法支持所有可能的位置。这将是理想的结构,但我不确定它是否支持这样的路线。

理想情况下,路由还支持通过 gem Friendly_Id 使用 slug,但目前这些仅适用于 Category 模型。

我该怎么做才能让我的路线像这样工作?

【问题讨论】:

    标签: ruby-on-rails database routes ruby-on-rails-5 ancestry


    【解决方案1】:
    get ':category/:location', to: 'locations#action1'
    get ':category/:location/:sublocation', to: 'locations#action2'
    get ':category/:location/:sublocation/:subsublocation', to: 'locations#action3'
    

    更多信息请参见Rails Routing from the Outside In

    【讨论】:

    猜你喜欢
    • 2014-05-17
    • 2018-02-27
    • 1970-01-01
    • 2014-12-31
    • 2013-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-22
    相关资源
    最近更新 更多