1

我有 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吗?

4

0 回答 0