我已经成功地使用accepts_nested_attributes_for
创建了依赖多态关联,但现在我想分配一个已经存在的多态。我得到错误:
undefined method `id for {"id"=>"4"}:ActiveSupport::HashWithIndifferentAccess
型号:
class Person < ActiveRecord::Base
has_one :address, :as => :addressable
end
class Company < ActiveRecord::Base
has_one :address, :as => :addressable
end
class Address < ActiveRecord::Base
belongs_to :addressable, :polymorphic => true
end
视图:
<%= form_for @company do |f| %>
<p>
<%= f.label :company_name %><br />
<%= f.text_field :company_name %>
</p>
<%= f.fields_for :address do |f| %>
<div class="field">
<%= f.collection_select :id, Address.all, :id, :description, {:prompt => "-Select Address"} %>
</div>
<% end %>
生成的params哈希为:
"company"=>{"company_name"=>"myCompanyName", "address"=>{"id"=>"4"}}
这在我看来是合理的。我做错了什么?