2012-03-28 178 views
1

在我的routes.rb我:Rails的嵌套資源和路徑

resources :boards do 
    resources :items 
    end 
現在

在板#表明我要顯示一個鏈接到http://mysite/baord/:board_id/items/new,運行rake routes我得到:

new_board_item GET /boards/:board_id/items/new(.:format)      items#new 

和所以我應該可以使用new_board_item_path但這隻適用於site.com/board/:board_id/items,但我想在board中使用此鏈接#show action,但它告訴我:

No route matches {:action=>"new", :controller=>"items"} 

雖然這不是真的!

+5

你的主板傳遞到板項目?例如。 new_board_item_path(@board) – 2012-03-28 15:47:21

+0

我的錯誤錯誤是...如果您將評論轉換爲答案,我會接受它 – 2012-03-28 15:51:02

+0

很高興我能幫上忙! :) – 2012-03-28 15:59:39

回答

1

傳遞父資源到路徑,所以在這種情況下:

new_board_item_path(@board) 
+0

完美......只有一個問題:爲什麼在相同的情況下這不是必要的?例如,如果我想添加一個鏈接到項目#新的項目#索引的視圖它的工作原理沒有傳遞董事會的方法? – 2012-03-28 16:20:23