【问题标题】:Rails nested rources and routes and how to set them up in route.rbRails 嵌套资源和路由以及如何在 route.rb 中设置它们
【发布时间】:2011-04-22 16:48:21
【问题描述】:

我认为我可以处理嵌套路由(很棒的 url 助手,以及访问等等)和带有 accept_nested_attributes_for 的表单中的嵌套资源,但是我在路由中使用什么:

resources :schools do
   resources :documents
end

还有

resources :schools :has_many => :documents
end

请告诉我这些之间的区别。
显然 has_many 是一对多的关系。它是否会产生路径助手并需要正确的路由,并且对于 do 块,这意味着什么关系,没有?只是路径助手(/schools/documents),如果我想要学校下的多个资源(除了书籍,比如文件)怎么办,第一种方法我可以将它添加到 do-end 块但是第二种方法呢,只有两行, 每个 has_many 一个?
虽然我已经阅读了指南和 api,但我并没有完全理解这里的区别/用法,任何可以清楚解释两者之间区别的人(以“a 做 x 而 b 做 y”的形式会很棒) 将不胜感激:)

哦,当然,它们与模型中的 has_many 有什么关系——所以我猜这些关系可以在带有 has_many 的模型、控制器(主要通过使用路径)和视图中(通过具有嵌套属性的表单) )。

【问题讨论】:

    标签: ruby-on-rails routes url-routing nested-forms nested-resources


    【解决方案1】:

    他们都做同样的事情,由你选择哪一个

    我更喜欢 do 块格式,因为它更易于阅读

    顺便说一句,使用 has_many 格式,您可以为多个嵌套路由执行 :has_many => [:docs, :otherthings]

    【讨论】:

      【解决方案2】:

      我认为 has_many 语法是添加到 Rails 2 中的,作为那些不喜欢块语法的人的简写。你可以看到一篇关于它的博客文章here。我刚刚尝试过,似乎 Rails 3 忽略了 has_many 选项。所以我的输出是:

      resources :schools do
         resources :documents
      end
      

      创建路线:

          school_documents GET    /schools/:school_id/documents(.:format)          {:action=>"index", :controller=>"documents"}
                           POST   /schools/:school_id/documents(.:format)          {:action=>"create", :controller=>"documents"}
       new_school_document GET    /schools/:school_id/documents/new(.:format)      {:action=>"new", :controller=>"documents"}
      edit_school_document GET    /schools/:school_id/documents/:id/edit(.:format) {:action=>"edit", :controller=>"documents"}
           school_document GET    /schools/:school_id/documents/:id(.:format)      {:action=>"show", :controller=>"documents"}
                           PUT    /schools/:school_id/documents/:id(.:format)      {:action=>"update", :controller=>"documents"}
                           DELETE /schools/:school_id/documents/:id(.:format)      {:action=>"destroy", :controller=>"documents"}
                   schools GET    /schools(.:format)                               {:action=>"index", :controller=>"schools"}
                           POST   /schools(.:format)                               {:action=>"create", :controller=>"schools"}
                new_school GET    /schools/new(.:format)                           {:action=>"new", :controller=>"schools"}
               edit_school GET    /schools/:id/edit(.:format)                      {:action=>"edit", :controller=>"schools"}
                    school GET    /schools/:id(.:format)                           {:action=>"show", :controller=>"schools"}
                           PUT    /schools/:id(.:format)                           {:action=>"update", :controller=>"schools"}
                           DELETE /schools/:id(.:format)                           {:action=>"destroy", :controller=>"schools"}
      

      同时

      resources :schools :has_many => :documents
      

      创建路线:

          schools GET    /schools(.:format)          {:action=>"index", :controller=>"schools"}
                  POST   /schools(.:format)          {:action=>"create", :controller=>"schools"}
       new_school GET    /schools/new(.:format)      {:action=>"new", :controller=>"schools"}
      edit_school GET    /schools/:id/edit(.:format) {:action=>"edit", :controller=>"schools"}
           school GET    /schools/:id(.:format)      {:action=>"show", :controller=>"schools"}
                  PUT    /schools/:id(.:format)      {:action=>"update", :controller=>"schools"}
                  DELETE /schools/:id(.:format)      {:action=>"destroy", :controller=>"schools"}
      

      我认为你的问题的真正答案是那些是/应该做同样的事情,只是使用不同的语法。

      【讨论】:

        猜你喜欢
        • 2016-07-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-12-09
        • 1970-01-01
        • 2016-04-05
        • 2011-05-23
        • 1970-01-01
        相关资源
        最近更新 更多