在以下情况下如何为多个嵌套属性创建表单和操作:
订单项:
has_many :item_options, :dependent => :destroy
has_many :product_options, :through => :item_options
产品选项:
belongs_to :product
belongs_to :option
has_many :item_options
has_many :line_items, :through => :item_options
项目选项:
attr_accessible :line_item_id, :product_option_id
belongs_to :line_item, :foreign_key => "line_item_id"
belongs_to :product_option,:foreign_key => "product_option_id"
当我创建新的 LineItem 时,我需要创建新的 ItemOption(s)。这是我的表格:
<%= form_for(LineItem.new) do |f| %>
<%= f.hidden_field :product_id, value: @product.id %>
<%= f.fields_for :item_options do |io| %>
<% @product.options.uniq.each do |o| %>
<%= o.name %>:
<%= io.collection_select :product_option_id, o.product_options.where(:product_id => @product.id), :id, :value %>
<% end %>
<%= f.submit %>
<% end %>
当我点击添加到购物车时,我得到:
ItemOption(#70296453751440) 预期,得到 Array(#70296430421140)
将 Accepts_nested_attributes_for :item_options 添加到 LineItem 时,我的选择未显示 :(
和
<%= select_tag "product_option_id", options_from_collection_for_select(o.product_options.where(:product_id => @product.id), :id, :value) %>
#item_options not created:
Parameters: {"utf8"=>"✓", "authenticity_token"=>"/WM5/MqPn1yCxjKWoJQmjfko2pR4RiYV0S2KeTTpA3w=", "line_item"=>{"product_id"=>"1"}, "product_option_id"=>"5", "commit"=>"add"}
最后一个,我创建了这样的动作:
@line_item = LineItem.new(params[:line_item])
@line_item.item_options.build
....
我哪里错了?:( 我完全糊涂了。ps.类似的问题Rails 3.2 has_many through form submit 这是表单: