如何使用has_one关联的集合选择

时间:2013-04-14 17:57:45

标签: select collections has-one

我有2个型号:

class Brigade < ActiveRecord::Base
  attr_accessible :title
  has_one :country
end

class Country < ActiveRecord::Base
  attr_accessible :title
end

在我的_form.html.erb中我有:

<%= form_for(@brigade) do |f| %>
     <p>
       <%= f.label :title %>
       <%= f.text_field :title %>
     </p>
     <p>
       <%= f.label "Country" %>
       <%= f.collection_select :country_id, Country.all, :id, :title %>
     </p>
<% end %>

运行这个我有一条消息:

undefined method `country_id' for #<Brigade:0x9a89cac> (ActionView::Template::Error)

我认为在这种情况下,Rails必须自动加入country_idbrigade,但事实并非如此。

我不知道我的错误在哪里。是否有必要使用accepts_nested_attributes_for

0 个答案:

没有答案