【问题标题】:Understanding Shallow in Nested Resources in Rails 4了解 Rails 4 中嵌套资源的浅层
【发布时间】:2016-01-18 16:35:58
【问题描述】:

我需要为 3 个模型做嵌套资源:

  • 家庭(狗、猫、鸟)
  • 品种(比格犬、拳击手...)
  • 动物

遵循 Rails 指南建议我不应该这样做:

  resources :families
    resources :breeds
      resources :animals
    end
  end

和“Rails Guides”建议改用“浅”,所以我应该这样做:

  resources :families, shallow: true do
    resources :breeds, shallow: true do
      resources :animals
    end
  end

我理解“指南”所说的“不嵌套成员操作”...仅:[:index, :new, :create] ...

但是它似乎只适用于最后 2 个嵌套资源,有 3 个嵌套资源(如我的情况)rake routes 给我:

Prefix:            breed_animals
Verb:              GET
URI Pattern:       /breeds/:breed_id/animals(.:format)
Controller#Action: animals#index

我期望在 URI 模式中:

URI Pattern:    /family/:family_id/breeds/:breed_id/animals(.:format)

¿我失去了什么?

非常感谢

【问题讨论】:

    标签: ruby-on-rails-4


    【解决方案1】:

    这是使用浅层路由时所期望的:

    /breeds/:breed_id

    这是肤浅的,因为无论您在哪个家庭,品种 ID 都是唯一的。

    因此,该品种的动物索引对于该路线也将是正确的,并且浅层路线的行为符合预期。

    【讨论】:

    • 好吧,我想我明白了,如果我有品种,家庭无关紧要,好吧。
    • 但是:我的代码正确吗?:资源:家庭,浅:真实做资源:品种,浅:真实做资源:动物结束结束
    • 嗯,我已经找到答案了:第二个, shallow: true do,不需要,谢谢
    猜你喜欢
    • 1970-01-01
    • 2015-11-30
    • 1970-01-01
    • 2014-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-01
    相关资源
    最近更新 更多