我的某个模型的语义形式出现问题。相关模型
class Event < ActiveRecord::Base
belongs_to :series
...
end
class Series < ActiveRecord::Base
has_many :events
...
end
当我在浏览器中访问/series/new
时,出现错误:
series_url failed to generate from {:controller=>"series", :action=>"show"} - you may have ambiguous routes, or you may need to supply additional parameters for this route. content_url has the following required parameters: ["series", :id] - are they all satisfied?
编辑:我已经运行rake routes | grep series | grep new
寻找有冲突的路由,而且没有,这是输出:
new_series_event GET /series/:series_id/events/new(.:format) {:controller=>"events", :action=>"new"}
new_series GET /series/new(.:format) {:controller=>"series", :action=>"new"}
与/series/new
对应的模板呈现以下部分:
<% semantic_form_for(@series) do |f| %>
<%= f.error_messages %>
<% f.inputs do %>
<%= f.input :title %>
<%= f.input :uri, :label => "URL of the Series", :hint => "For example, use 'tes' for 'Transportation Education Series'. It will appear as http://events.kittelson.com/tes" <%= f.input :description %>
<%= f.input :contact, :label => "Contact email" %>
<%= f.input :color, :label => "Pick a dark color" %>
<% end %>
<% f.buttons do %>
<%= f.commit_button %>
<%= link_to "or cancel", :back %>
<% end %>
<% end %>
@series
对象在控制器中定义为Series.new
。
我不明白这与路由有什么关系,我跑rake routes
并且只有一个控制器操作映射到/series/new
。
以下是config/routes.rb
与这些模型相关的部分:
ActionController::Routing::Routes.draw do |map|
map.resources :series, :has_many => :events
map.resources :events, :has_many => :rsvps
end
可能导致此路由错误的原因是什么?
答案 0 :(得分:1)
只是预感。你可能会混淆Rail的变形器。也许它应该是belongs_to serial
而不是belongs_to series
?
你知道,通常我们会写belongs_to user
而不是users
。所以你可能想尝试一下。