1

我想我有一個處理嵌套的路線(偉大的網址助手和訪問等等)和嵌套的資源在形式與accept_nested_attributes_for但我該怎麼用在路線,因爲我看到兩個:Rails嵌套的rources和路線,以及如何在route.rb設置它們

resources :schools do 
    resources :documents 
end 

resources :schools :has_many => :documents 
end 

請你能告訴我它們之間的差異。
顯然has_many是一對多的關係。它是否會產生路徑幫助程序,並且需要正確的路由,並且爲了阻止這種暗示的關係,沒有?只是路徑助手(/學校/文件),以及如果我想要在學校中使用多種資源(書籍,文檔等),我可以將其添加到do-end模塊中的第一種方式,但第二種方法,只有兩行,每個has_many一個?
雖然我讀過的指南和API的我不太來到這裏的區別/使用,任何人都可以提供兩者之間的區別的一個明確的解釋(形式爲「一個做X,而B則Y」會很好):)

哦,當然它們與模型中的has_many有什麼關係 - 所以我想這些關係可以在has_many模型中,控制器(主要是通過使用路徑)並在視圖中(通過具有嵌套屬性的表單)。

回答

1

他們都做同樣的事情,它由你來選擇,我更喜歡哪一個

的做塊格式作爲其更易於閱讀

BTW用的has_many格式,你可以對多個嵌套做:has_many => [:docs, :otherthings]路線

+0

愛btw提示,這是非常helpul,thx! – 2011-04-23 20:49:58

0

我認爲的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"} 

我認爲真正的答案你的問題是那些重新/本來應該做同樣的事情,只是用不同的語法。