上設置我有2種型號Order和OrderItem的。在訂單#新我嵌套了一個新的OrderItem。當我創建訂單時,它將被保存,但不是OrderItem。看起來像order_id沒有在OrderItem中設置。ID不是嵌套形式
Order has_many :order_items
accepts_nested_attributes_for :order_items, :reject_if => lambda { |a| a[:product_id].blank? }
attr_accessible :id, :date, :note, :client_id, :order_items_attributes
在OrderItem的
belongs_to :order
attr_accessible :product_id, :order_id, :quantity, :color
validates_presence_of :product_id, :order_id, :quantity
在我看來
<%= nested_form_for @order do |f| %>
<%= f.fields_for :order_items do |item| %>
<%= item.label :product_id %> <%= collection_select(:order_item, :product_id, Product.all, :id, :name, :prompt => 'Select Product') %><br/>
<%= item.label :quantity %> <%= item.number_field :quantity, :value => '1', :min => '1' %><br />
<%= item.label :color_id %> <%= collection_select(:order_item, :color_id, Color.all, :id, :name, :prompt => 'Select Color') %>
<%= item.link_to_remove "Remove this item" %>
<% end %>
<p><%= f.link_to_add "Add an item", :order_items %></p>
<% end %>
服務器日誌返回"order"=>{"client_id"=>"1", "note"=>"efewfwe", "order_items_attributes"=>{"0"=>{"quantity"=>"1", "_destroy"=>"false"}}}, "commit"=>"Create Order", "order_items"=>{"order_id"=>""}, "order_item"=>{"product_id"=>"12", "color_id"=>"4"}}
不知道什麼是第一個零種手段,而是ORDER_ID是零
是什麼在訂單控制器的創建方法是什麼樣子? – janders223
發佈保存訂單的操作源。您可以通過分配順序屬性'order.attributes = params [:order]'來檢查順序對象,然後檢查順序實例有什麼問題。 – Hck
@ janders223'類OrdersController
Gaelle