我正在使用 Rails 3.1 和 Ruby 1.9.2 以及 Active Admin 来构建 CMS。这是我的 Place 和 Image 模型:
class Place < ActiveRecord::Base
has_one :image
accepts_nested_attributes_for :image
end
class Image < ActiveRecord::Base
belongs_to :place
end
这是我在 Places 控制器的“新”操作中呈现的 Formtastic 表单:
<%= semantic_form_for [:admin, @place] do |p| %>
<%= p.inputs "Details" do %>
<%= p.input :name %>
<%= p.input :description %>
<%= p.input :phone %>
<%= p.input :address %>
<%= p.input :image %>
<% end %>
<%= p.buttons %>
<% end %>
当我在浏览器中加载表单时,我看到以下错误:
undefined method `place_id' for #<Place:0xb801744>
这是踢球者:在我的 Place 模型中,如果我更改has_one :image
为has_many :images
和accepts_nested_attributes_for :image
,accepts_nested_attributes_for :images
并且在我的表单中更改p.input :image
为p.input :images
,则错误消失并且 Formtastic 正确呈现包含所有可用图像对象的多选输入元素。那么,当我使用has_one
关联而不是查看选择输入元素时,为什么会出现此错误?