1

I think I have a handle on nested routes (great url helpers, and access and much more) and nested resources in forms with accepts_nested_attributes_for but what do I use in routes as I see both:

resources :schools do
   resources :documents
end

and also

resources :schools :has_many => :documents
end

please can you tell me the difference between these.
Obviously has_many is for a one-to-many relationship. does it produce path helpers and require correct routing and for the do block what relationship does that imply, none? just path helpers (/schools/documents) and what if I want multiple resources (other than books, say documents) under schools, the first way I can add it into the do-end block but what about the second way, just two lines, one for each has_many?
Though I've read the guides and api's I don't quite get the difference/usage here and anyone that can provide a clear explanation of the distinction between the two (in the form 'a does x whereas b does y' would be great) would be much appreciated :)

Oh and of course how they relate to having the has_many in the model - so I guess these relationships can be in the model with has_many, the controller (mostly thru usage of paths) and in the view (through forms with nested attributes).

4

2 回答 2

1

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

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

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

于 2011-04-22T19:29:55.403 回答
0

我认为 has_many 语法是添加到 Rails 2 中的,作为那些不喜欢块语法的人的简写。您可以在此处查看有关它的博客文章。我刚刚尝试过,似乎 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"}

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

于 2011-04-22T19:39:17.937 回答