Domanda

I have 3 nested resources: sources, twitter_source and twitter_aggregation_methods.

The relationship of their respective models is as follows:

sources.rb

has_many :twitter_sources

has_many :rss_sources

twitter_source.rb

has_many :twitter_aggregation_methods

belongs_to :source

twitter_aggregation_method.rb

belongs_to :twitter_source

The config/routes.rb is set up as follows:

resources :sources do
 resources :rss_sources
   resources :twitter_sources do
     resources :twitter_aggregation_methods
     resources :influencer_trends do
       resources :trends
     end
   end
end

When I try to add a new twitter_aggreggation_method I get the following error:

NoMethodError in TwitterAggregationMethods#new

Showing /home/notebook/work/abacus/app/views/twitter_aggregation_methods/_form.html.erb where line #1 raised:

undefined method `twitter_source_twitter_aggregation_methods_path' for #<#<Class:0xb982e10>:0xc104ef4>

My views/twitter_aggregation_methods/_form.html.erb is as follows:

<%= form_for([@twitter_source, @twitter_aggregation_method]) do |f| %>
  <% if @twitter_aggregation_method.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@twitter_aggregation_method.errors.count, "error") %> prohibited this twitter_aggregation_method from being saved:</h2>

  <ul>
  <% @twitter_aggregation_method.errors.full_messages.each do |msg| %>
    <li><%= msg %></li>
  <% end %>
  </ul>
</div>
<% end %>

<div class="field">
  <%= f.label :twitter_source_id %><br>
  <%= f.text_field :twitter_source_id, value: @twitter_source.id %>
</div>
<div class="field">
  <%= f.label :aggregation_method %><br>
  <%= f.text_field :aggregation_method %>
</div>
<div class="actions">
<%= f.submit %>

The error points to the first line of this partial.

È stato utile?

Soluzione

Looking at the resources defined, twitter_source_twitter_aggregation_methods_path doesn't exist. Hence, the error. You can do rake routes and check the prefixes to know what all routes exist.

From TwitterAggregationMethods#new you would be expecting to go to TwitterAggregationMethods#create, for that pass an instance of source in the form,

Replace

<%= form_for([@twitter_source, @twitter_aggregation_method]) do |f| %>

with

<%= form_for([@source, @twitter_source, @twitter_aggregation_method]) do |f| %>

and set @source in the TwitterAggregationMethods#new.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top