【问题标题】:Rails 3 level deep nested resourcesRails 3 级深度嵌套资源
【发布时间】:2012-05-19 00:25:00
【问题描述】:

我知道许多 Rails 开发人员说将资源嵌套超过 2 层是错误的行为。我也同意,因为当您的网址看起来像 mysite.com/account/1/people/1/notes/1 时,它会变得混乱。我正在尝试找到一种使用嵌套资源但不将它们嵌套 3 层的方法。

这是错误的做法,因为 Rails 开发人员不推荐它,而且很难弄清楚如何将它嵌套在控制器或表单视图中。

resources :account do 
  resources :people do
    resources :notes
  end
end

rails 开发人员说应该这样做的正确方法是这样的

resources :account do 
  resources :people
end

resources :people do
  resources :notes
end

这是我经常遇到的问题。每当我访问 account/1/people 时,我都可以将一个人添加到该帐户中,并且可以说 URL 类似于 mysite.com/account/1/people/1 并且效果很好。

现在,如果我尝试从帐户 1 访问 mysite.com/people/1/notes,我会收到错误提示

找不到没有帐户 ID 的人

如何让它正常工作?

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3


    【解决方案1】:

    您可以尽可能深地嵌套路由,因为 rails 3.x 允许您使用 shallow: true 将它们展平

    尝试一下

    resources :account, shallow: true do 
      resources :people do
        resources :notes
      end
    end
    

    使用 rake 路由来看看你得到了什么 :)

    更新以回应评论

    正如我所说,使用 rake 路由看看你能得到什么 url

    resources :account, shallow: true do 
      resources :people, shallow: true do
        resources :notes
      end
    end
    

    为您提供这些路线

    :~/Development/rails/routing_test$ rake routes
          person_notes GET    /people/:person_id/notes(.:format)        notes#index
                       POST   /people/:person_id/notes(.:format)        notes#create
       new_person_note GET    /people/:person_id/notes/new(.:format)    notes#new
             edit_note GET    /notes/:id/edit(.:format)                 notes#edit
                  note GET    /notes/:id(.:format)                      notes#show
                       PUT    /notes/:id(.:format)                      notes#update
                       DELETE /notes/:id(.:format)                      notes#destroy
        account_people GET    /account/:account_id/people(.:format)     people#index
                       POST   /account/:account_id/people(.:format)     people#create
    new_account_person GET    /account/:account_id/people/new(.:format) people#new
           edit_person GET    /people/:id/edit(.:format)                people#edit
                person GET    /people/:id(.:format)                     people#show
                       PUT    /people/:id(.:format)                     people#update
                       DELETE /people/:id(.:format)                     people#destroy
         account_index GET    /account(.:format)                        account#index
                       POST   /account(.:format)                        account#create
           new_account GET    /account/new(.:format)                    account#new
          edit_account GET    /account/:id/edit(.:format)               account#edit
               account GET    /account/:id(.:format)                    account#show
                       PUT    /account/:id(.:format)                    account#update
                       DELETE /account/:id(.:format)                    account#destroy
    

    可以看出,您可以访问您决定需要的任何级别的所有模型。其余的取决于您在控制器操作中添加的任何内容。

    您确实必须处理操作以确保在未传入 id 参数时采取适当的操作,因此如果您为特定模型使用 id,请检查该 id 是否在参数列表中,如果不采取替代行动。例如如果您没有传递帐户 ID,请确保您不要尝试使用它

    您的评论表明您已经使用了浅层路线,但这不是您在问题中发布的内容?

    【讨论】:

    • 我目前在我的路线文件中有浅真实的内容,只是不确定如何在没有帐户 ID 的情况下访问人员/笔记。 Rails 抛出此错误Couldn't find Account without an ID
    猜你喜欢
    • 2011-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多