1

我的一个模型的语义形式有问题。相关型号为

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

什么可能导致此路由错误?

4

1 回答 1

1

只是一种预感。你可能会混淆 Rail 的变形器。也许它应该belongs_to serial代替belongs_to series?

你看,通常我们会写belongs_to user而不是users. 所以你可能想尝试一下。

于 2012-07-28T03:47:24.823 回答