2016-03-09 61 views
0

我有一個鏈接調用我的筆記屬於我的筆記本,像這樣:URL生成錯誤的Rails:沒有路由匹配

== link_to "#{note.title.upcase}", notebook_note_path(note), id: "note_name" 

我不知道爲什麼我收到錯誤。

這裏是我的路線:

resources :notebooks do 
    resources :notes 
end 
+0

您是否在routes.rb中創建了'notebook_note_path'? –

+0

我剛剛用我的路線編輯了q。我認爲路線是正確的,除非我誤解了一些東西。 –

回答

0

既然你已經嵌套的路線,你需要通兩個對象的helper方法。而不僅僅是一個note,試圖傳遞一個notebooknote

notebook_note_path(note.notebook, note) 

作爲參考,看看從rake routes輸出,當你有routes.rb設置爲你做:

notebook_notes GET /notebooks/:notebook_id/notes(.:format)   notes#index 
        POST /notebooks/:notebook_id/notes(.:format)   notes#create 
new_notebook_note GET /notebooks/:notebook_id/notes/new(.:format)  notes#new 
edit_notebook_note GET /notebooks/:notebook_id/notes/:id/edit(.:format) notes#edit 
    notebook_note GET /notebooks/:notebook_id/notes/:id(.:format)  notes#show 
        PATCH /notebooks/:notebook_id/notes/:id(.:format)  notes#update 
        PUT /notebooks/:notebook_id/notes/:id(.:format)  notes#update 
        DELETE /notebooks/:notebook_id/notes/:id(.:format)  notes#destroy 
... 

由於此路徑的URI爲/notebooks/:notebook_id/notes/:id,因此必須同時存在一個筆記本ID和一個備註ID(Rails會使用傳入輔助方法的資源的ID將其前面的冒號替換爲前面的冒號)。

+0

救了我!謝謝一堆。 –

相關問題