问题
Can anyone help me to fix this?Thanks in advance! I get the following error:
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
Trace of template inclusion: 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'
And the file _form.html.erb is
<%= 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 %>
And my app/views/discussions/new.html.erb is,
<h1>New discussion</h1>
<%= render 'form' %>
<%= link_to 'Back', discussions_path %>
routes.rb file
Aggregator::Application.routes.draw do
get "welcome/index"
root 'welcome#index'
resources:discussions do
resources :comments
end
end
回答1:
You're form_for
is set to an array (@discussions
) when it should be to a single instance (@discussion
).
In your DiscussionsController's new method, ensure you have something along the following:
@discussion = Discussion.new
Then, in your form, change your current form_for expression to:
form_for @discussion
See if that clears your error.
回答2:
First of all you should correct your typo mistake in routes.rb
.
It should looks like (I have added a space between resources
and :discussions
):
Aggregator::Application.routes.draw do
get "welcome/index"
root 'welcome#index'
resources :discussions do
resources :comments
end
end
As Craig Kaminsky said, it seems you are having @discussions
as an array of discussion
records.'
In your controller's action new
, you should initialize your object as:
@discussion = Discussion.new
And you form should use same instance variable (@discussion
)
<%= form_for @discussion do |f| %>
<% if @discussion.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@discussion.errors.count, "error") %> prohibited
...
<% end %>
...
...
<% end %>
来源:https://stackoverflow.com/questions/22143309/nomethoderror-in-discussionsnew