2012-11-05 25 views
0

如何創建用於多個嵌套屬性形式和行動,如果:的Rails 3.2:從一個模型中的多個嵌套的數據

的LineItem:

has_many :item_options, :dependent => :destroy 
has_many :product_options, :through => :item_options 

ProductOption:

belongs_to :product 
belongs_to :option 
has_many :item_options 
has_many :line_items, :through => :item_options 

ItemOption:

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)預期,得到了陣列(#70296430421140)

當添加accepts_nested_attributes_for:item_options到LineItem的,我的選擇沒有diplayed :(

隨着

<%= 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 submission 這是形式:

回答

1

看起來這行:

Parameters: {"utf8"=>"✓", "authenticity_token"=>"/WM5/MqPn1yCxjKWoJQmjfko2pR4RiYV0S2KeTTpA3w=", "line_item"=>{"product_id"=>"1"}, "product_option_id"=>"5", "commit"=>"add"} 

參數product_option_id超出line_item哈希,並會也許你需要寫這樣的選擇:

<%= select_tag "line_item[product_option_id]", options_from_collection_for_select(o.product_options.where(:product_id => @product.id), :id, :value) %> 

我不確定,但也許是這樣。也許我需要更多的信息,比如失敗的確切路線。


額外的:foreign_key => "line_item_id":foreign_key => "product_option_id"沒有necesary,因爲在belongs_to型號名稱是相同的,將使用這些foreign_key。來自api

指定用於關聯的外鍵。默認情況下,這是 被猜測爲與「_id」後綴關聯的名稱。因此 定義**belongs_to :person**關聯的類將使用 「person_id」作爲默認值:foreign_key。同樣,belongs_to :favorite_person,:class_name =>「Person」將使用外鍵 「favorite_person_id」。


編輯

很抱歉,unknown attribute: product_option_id是因爲屬性名稱是product_option _ids,並且是一個數組,而不是一個獨特的價值。對於has_many關係中,名是collection_singular _ids,並選擇應該是:

<%= select_tag "line_item[product_option_ids][]", options_from_collection_for_select(o.product_options.where(:product_id => @product.id), :id, :value) %> 

這應該工作,我想:)...

+0

TNX,但'未知屬性: product_option_id' ...在參數:{{「utf8」=>「✓」, 「authenticity_token」=>「ik5ad6KsTOD + b4e9ioenFqfXY1nojXdEGAeNAXlCN8E =」, 「line_item」=> {「product_id」=>「1」, 「 「product_option_id」=>「5」}, 「commit」=>「添加數字」}' –

+0

也許它是這樣的:'<%= select_tag「item_option [product_option_id]」,options_from_collection_for_select(o.product_options.where(:product_id => @ product.id),:id,:value)%>'但它只是一個item_option,有幾種形式:(參數:''line_item「=> {」product_id「=>」1「},」item_option「=> {」product_option_id「=>」4「},' –

+0

這個變種更好:'< %= select_tag「item_option [] [product_option_id]」,options_from_collection_for_select(o.product_options.where(:product_id => @ product.id),::id,:value)%>',比參數:''line_item「=> {「product_id」=>「1」},「item_option」=> [{「product_option_id」=>「1」},{「product_option_id」=>「4」}]'。但是仍然沒有保存( –

相關問題