0

这个问题看起来很简单,但我似乎无法解决它。我一定遗漏了一些明显的东西,但非常感谢任何帮助!

出现的错误信息:

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
4

1 回答 1

2

对于嵌套资源,您需要将数组传递给form_for论坛和讨论,例如@forum在您的控制器中设置,然后在视图中使用:

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

rails 文档

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.
于 2012-04-14T15:34:13.400 回答