查看
<%= form_for @product, :url => admin_products_path do |f| %>
<div>
<%= f.label :name %>
<%= f.text_field :name %>
</div>
<div>
<%= f.label :description %>
<%= f.text_area :description, :rows => 7 %>
</div>
<%= f.fields_for :address do |a| %>
<p><%= a.label "Address One" %><br />
<%= a.text_field :address_one %></p>
<p><%= a.label "Address Two" %><br />
<%= a.text_field :address_two %></p>
<p><%= a.label :city %><br />
<%= a.text_field :city %></p>
<p><%= a.label :postcode %><br />
<%= a.text_field :postcode %></p>
<p><%= a.label :country %><br />
<%= a.select :country_id, Country.active.collect {|p| [ p.printable_name, p.id ] } %></p>
<% end %>
<% end %>
控制器
def create
@product = Product.new(params[:product])
if @product.save
flash[:notice] = 'Product was successfully created.'
render 'show'
else
render 'new'
end
end
模型
class Product < ActiveRecord::Base
has_one :address, :as => :addressable
end
但保存数据时显示问题:
Address(#93165130) expected,got ActiveSupport::HashWithIndifferentAccess(#79365750)
请帮我解决这个问题
答案 0 :(得分:6)
尝试添加accepts_nested_attributes_for :address
解决此问题。
答案 1 :(得分:0)
accepts_nested_attributes_for :address
如果不起作用,请尝试同时添加accepts_nested_attributes_for:地址
attr_accessor :address