任何人都可以帮我解决这个问题吗?先谢谢! 我收到以下错误:
NoMethodError in Discussions#new
Showing C:/Users/punitha/aggregator/app/views/discussions/_form.html.erb where line #1 raised:
undefined method `discussions_index_path' for #<#<Class:0x4375758>:0x437d6d8>
Extracted source (around line #1):
1 <%= form_for @discussions do |f| %>
2 <% if @discussions.errors.any? %>
3 <div id="error_explanation">
4 <h2><%= pluralize(@discussions.errors.count, "error") %> prohibited
模板包含跟踪:app/views/discussions/new.html.erb
Rails.root:C:/Users/punitha/aggregator
Application Trace | Framework Trace | Full Trace
app/views/discussions/_form.html.erb:1:in `_app_views_discussions__form_html_erb___1058370717_35436840'
app/views/discussions/new.html.erb:3:in `_app_views_discussions_new_html_erb__298093787_35389404'
文件_form.html.erb是
<%= form_for @discussions do |f| %>
<% if @discussions.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@discussions.errors.count, "error") %> prohibited
this discussion from being saved:</h2>
<ul>
<% @discussions.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<p>
<%= f.label :source_url %><br>
<%= f.text_field :source_url %>
</p>
<p>
<%= f.label :discussion_id %><br>
<%= f.text_area :discussion_id %>
</p>
<p>
<%= f.label :discussion_msg %><br>
<%= f.text_area :discussion_msg %>
</p>
<p>
<%= f.label :discussion_type %><br>
<%= f.text_field :discussion_type %>
</p>
<p>
<%= f.label :discussion_link %><br>
<%= f.text_area :discussion_link %>
</p>
<p>
<%= f.label :discussion_thumb %><br>
<%= f.text_area :discussion_thumb %>
</p>
<p>
<%= f.label :discussion_permalink %><br>
<%= f.text_area :discussion_permalink %>
</p>
<p>
<%= f.label :discussion_likecount %><br>
<%= f.number_field :discussion_likecount%>
</p>
<p>
<%= f.label :comment %><br>
<%= f.text_area :comment%>
</p>
<p>
<%= f.label :source_id %><br>
<%= f.number_field :source_id%>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
我的app / views / discussion / new.html.erb是,
<h1>New discussion</h1>
<%= render 'form' %>
<%= link_to 'Back', discussions_path %>
routes.rb文件
Aggregator::Application.routes.draw do
get "welcome/index"
root 'welcome#index'
resources:discussions do
resources :comments
end
end
答案 0 :(得分:0)
您form_for
设置为数组(@discussions
)时应该是单个实例(@discussion
)。
在您的DiscussionsController的新方法中,请确保您拥有以下内容:
@discussion = Discussion.new
然后,在表单中,将当前的form_for表达式更改为:
form_for @discussion
看看是否能解决您的错误。
答案 1 :(得分:0)
首先,你应该纠正routes.rb
中的拼写错误。
应该看起来像(我在resources
和:discussions
之间添加了一个空格):
Aggregator::Application.routes.draw do
get "welcome/index"
root 'welcome#index'
resources :discussions do
resources :comments
end
end
正如克雷格·卡明斯基所说的那样,你似乎将@discussions
作为一系列discussion
条记录。&#39;
在您的控制器的操作new
中,您应该将对象初始化为:
@discussion = Discussion.new
您的表单应使用相同的实例变量(@discussion
)
<%= form_for @discussion do |f| %>
<% if @discussion.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@discussion.errors.count, "error") %> prohibited
...
<% end %>
...
...
<% end %>