以下職位是基於on Rails的4軌道4 [最佳做法]嵌套資源和淺:真
實際上,我在尋找有關的多個嵌套資源良好的最佳實踐(大於1),和選項淺:真。
首先在我的路線,有這樣的:
resources :projects do
resources :collections
end
相關的路線是:
:
project_collections GET /projects/:project_id/collections(.:format) collections#index POST /projects/:project_id/collections(.:format) collections#create new_project_collection GET /projects/:project_id/collections/new(.:format) collections#new edit_project_collection GET /projects/:project_id/collections/:id/edit(.:format) collections#edit project_collection GET /projects/:project_id/collections/:id(.:format) collections#show PATCH /projects/:project_id/collections/:id(.:format) collections#update PUT /projects/:project_id/collections/:id(.:format) collections#update DELETE /projects/:project_id/collections/:id(.:format) collections#destroy projects GET /projects(.:format) projects#index POST /projects(.:format) projects#create new_project GET /projects/new(.:format) projects#new edit_project GET /projects/:id/edit(.:format) projects#edit project GET /projects/:id(.:format) projects#show PATCH /projects/:id(.:format) projects#update PUT /projects/:id(.:format) projects#update DELETE /projects/:id(.:format) projects#destroy
我有關嵌套資源的限制的文件中讀取資源不應該嵌套超過1級。
來源:http://guides.rubyonrails.org/routing.html#limits-to-nesting 好的。然後,就像文件說的那樣,我會在我的路線中使用「淺」。
shallow do
resources :projects do
resources :collections
end
end
相關的路線是:
project_collections GET /projects/:project_id/collections(.:format) collections#index
POST /projects/:project_id/collections(.:format) collections#create
new_project_collection GET /projects/:project_id/collections/new(.:format) collections#new
edit_collection GET /collections/:id/edit(.:format) collections#edit
collection GET /collections/:id(.:format) collections#show
PATCH /collections/:id(.:format) collections#update
PUT /collections/:id(.:format) collections#update
DELETE /collections/:id(.:format) collections#destroy
projects GET /projects(.:format) projects#index
POST /projects(.:format) projects#create
new_project GET /projects/new(.:format) projects#new
edit_project GET /projects/:id/edit(.:format) projects#edit
project GET /projects/:id(.:format) projects#show
PATCH /projects/:id(.:format) projects#update
PUT /projects/:id(.:format) projects#update
DELETE /projects/:id(.:format) projects#destroy
主要的區別我看到的是收藏的 「秀」,這樣一句:
collection GET /collections/:id(.:format) collections#show
所以,如果我我是對的,展示動作的鏈接是:
<%= link_to 'Show", collection_path(collection)%>
,應該返回類似這樣的內容:「http://example.com/collections/1」
但是! 2件事:
- 這是行不通的。我得到的是「http://example.com/projects/1」。 WTF?
- 即使是工作,它實際上是非常糟糕的,因爲我失去了REST基本是說:「收藏是項目的孩子,那麼URL應該是‘本地主機/項目/ 1 /收藏/ 1’
我不明白什麼是淺薄的利益,如果它是放鬆休息行動的巨大優勢。什麼是興趣?什麼是放鬆「顯示」行動的興趣?我已經發布到SO,但唯一的評論我得到的是「這是正常的東西。」跆拳道?在這是一個正常的行爲,以「從其他API刪除」行動?
我轉載了一箇中性項目的問題,以確保我沒有做有問題,併發生同樣的問題。所以,對於輔助者來說,使用淺度可能會比較方便,但對於其他人來說,這並不方便,因爲您放棄了「一個集合嵌套到一個項目中的所有興趣,所以這反映在URL中」。
我不知道是否有另一種方法可以做到這一點,淺的確允許助手具有更多的靈活性,但它是錯誤的,它是休息兼容的。那麼,是否有機會讓「幫助者」工作(擁有「nested3_path(collection)」而非「nested1_nested2_nested3([nested1.nested2.nested3,nested1.nested2,nested1])」,並保持「 url部分「並保持」nested1/123/nested2/456/nested3/789?
謝謝!
您是否嘗試過重新啓動服務器以使路由生效? 據商務部 '資源:帖子,淺:真正做 資源:評論 end' 會產生 '資源:帖子做 資源:評論,除了:[:顯示,編輯,:更新, :destroy] end 資源:評論,只:[:顯示,:編輯,:更新,:銷燬]' 你好像在做什麼 – jamesy829
確實必須重啓服務器才能使路由生效。 – 0112