Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails RubyonRails未定义的方法“讨论路径”-嵌套资源,一对多关系_Ruby On Rails_Ruby On Rails 3_Nested Forms - Fatal编程技术网

Ruby on rails RubyonRails未定义的方法“讨论路径”-嵌套资源,一对多关系

Ruby on rails RubyonRails未定义的方法“讨论路径”-嵌套资源,一对多关系,ruby-on-rails,ruby-on-rails-3,nested-forms,Ruby On Rails,Ruby On Rails 3,Nested Forms,这个问题看起来很简单,但我就是解决不了。我肯定错过了一些明显的东西,但我真的非常感谢任何帮助 出现的错误消息: undefined method `discussions_path' for #<#<Class:0x000001032e90d8>:0x000001030f1168> 讨论/_form.html.erb文件: <%= form_for [@forum, @discussion] do |f| %> <%= f.error_messa

这个问题看起来很简单,但我就是解决不了。我肯定错过了一些明显的东西,但我真的非常感谢任何帮助

出现的错误消息:

undefined method `discussions_path' for #<#<Class:0x000001032e90d8>:0x000001030f1168>
讨论/_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 %>
这是我现在进入终端的全部错误:

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

对于嵌套资源,您需要传入一个数组以形成论坛和讨论的_,例如,在控制器中设置@forum,然后在视图中使用:

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


嗯,我确实认为两种方法都可以通过,但我似乎得到了相同的错误:未定义的方法路径。My _form.html.erb现在从以下内容开始:My discussions controller新操作现在是:def new@discussion=discussion.new@forum=forum.find_by_permalinkparams[:id]end-请注意,我在URL中使用永久链接而不是ID。我不这么认为-但我已经用添加的建议代码更新了我的原始问题-以及当我尝试加载/forums/general chat/discussions/new时从终端显示的完整错误。日志中的查询是:选择论坛。*from forums WHERE forums.permalink为NULL LIMIT 1,因此,除非你有一个论坛的permalink为空,否则@forum将被设置为nil,这将导致form_for失败。我想你想要:@forum=forum.find\u by\u permalinkparams[:forum\u id]完美!这很有道理,非常感谢你的帮助!
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
<%= form_for [@forum, @discussion] do |f| %>
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.