2012-04-14 25 views
0

這個問題似乎很簡單,但我似乎無法解決它。我一定會錯過一些明顯的東西,但是真的很感謝任何幫助!Ruby on Rails未定義的方法`discuss_path' - 嵌套的資源,一對多的關係

undefined method `discussions_path' for #<#<Class:0x000001032e90d8>:0x000001030f1168> 

路線文件:

resources :forums do 
    resources :discussions 
end 

討論/ _form.html.erb文件:

<%= form_for [@forum, @discussion] do |f| %> 
<%= f.error_messages %> 
<p> 
    <%= f.label :name %><br /> 
    <%= f.text_field :name %> 
</p> 
<p><%= f.submit "Submit" %></p> 
<% end %> 

討論控制器新的動作:

出現

錯誤消息

def new 
    @forum = Forum.find_by_permalink(params[:id]) 
    @discussion = Discussion.new 
end 

這是完全錯誤,現在我得到(在終端中):

Started GET "/forums/general-chat/discussions/new" for 127.0.0.1 at 2012-04-14 18:35:28 +0100 
Processing by DiscussionsController#new as HTML 
Parameters: {"forum_id"=>"general-chat"} 
Forum Load (1.6ms) SELECT "forums".* FROM "forums" WHERE "forums"."permalink" IS NULL LIMIT 1 
Rendered layouts/_content_full_start.html.erb (0.0ms) 
Rendered discussions/_form.html.erb (574.4ms) 
Rendered discussions/new.html.erb within layouts/application (575.9ms) 
Completed 500 Internal Server Error in 581ms 

回答

2

對於需要在傳遞一個數組form_for與論壇和討論,例如嵌套資源設置@forum在你的控制器,然後在視圖中使用:

<%= form_for [@forum, @discussion] do |f| %> 

rails docs

If your resource has associations defined, for example, you want to add comments 
to the document given that the routes are set correctly: 
<%= form_for([@document, @comment]) do |f| %> 
... 
<% end %> 
Where @document = Document.find(params[:id]) and @comment = Comment.new. 
+0

嗯,我也想通過這兩個會出現這種情況,但我似乎得到相同錯誤:未定義的方法discussion_path。我的_form.html.erb現在以這個開始:'<%= form_for [@forum,@discussion] do | f | %>'和我的討論控制器新的行動是現在:'def new @discussion = Discussion.new @forum = Forum.find_by_permalink(params [:id]) end' - 我要使用永久鏈接,而不是ID在URL中。 – Kobius 2012-04-14 16:45:33

+2

你確定'@ forum'沒有被設置爲零嗎? – pjumble 2012-04-14 17:08:37

+0

我不這麼認爲 - 但我已經更新了我原來的問題,並添加了建議的代碼 - 以及當我嘗試加載/論壇/一般聊天/討論/新 – Kobius 2012-04-14 17:38:14

相關問題