データベースに投稿するための追加の属性を取得できないhas_many:throughフォームがあります。パラメータ名をどこかで汚しています。投稿する外部キーを取得できますが、結合テーブルで追跡しようとしている別の属性があります。これは100%ajaxベースのフォームであることに注意してください。これが私が知っていることです
編集:同様の問題を調査した後、フォーム属性を作成することになっていることは理解しましたが、見つけたコードは何らかの理由で機能しません。ここにいくつかのリソースがあります。
http://railsforum.com/viewtopic.php?id=20203
Rails 3、ネストされたマルチレベルフォームおよびhas_manyから
私が理解していないのは、product_idsのアタッチがレールに組み込まれていることです。それらの値は投稿します。その配列にquantity_shipped属性をアタッチするのが難しいのはなぜですか?
Models and Relationships
Shipment has_many :products :through => :product_shipments
Product has_many :shipments :through => :product_shipments
ProductShipments belongs_to :shipment, belongs_to :product
ProductShipments table
t.integer :shipment_id
t.integer :product_id
t.integer :qty_shipped <-- This is the Problem Child
このパーシャルは、特定のベンダーのすべての製品を表示するために数回ループされます。product_idsの配列と、product_shipments数量の配列を生成します。
_product_shipments.html.erb。
<div id="product_shipments_container">
<h3>Assign Products to Ship</h3>
<ul class="product_shipments" id="product_shipments">
<% Product.by_client(@client_id).each do |product| %>
<%= content_tag_for :li, product, :value => product.id do %>
<%= hidden_field_tag("shipment[product_ids][]", product.id) %>
<%= product.product_name %><%= text_field_tag("product_shipments[qty_shipped]")%> <--This is where the issue lies
<% end %>
<% end %>
</ul>
</div>
これは、フォームが送信されたときに関連するPOSTデータです
"product_ids"=>["1", "3"]}, "product_shipments"=>{"qty_shipped"=>["32", "23"]}
これはデータベースに送信されるSQLです
INSERT INTO `product_shipments` (`product_id`, `qty_shipped`, `shipment_id`)
VALUES (1, NULL, 155)
INSERT INTO `product_shipments` (`product_id`, `qty_shipped`, `shipment_id`)
VALUES (3, NULL, 155)
これが私のコントローラーのアクションです
def create
@shipment = Shipment.new(params[:shipment])
@product_shipments = @shipment.product_shipments.build(params[:product_shipments])
[:qty_shipped])<-正しくありませんが、@ shipment.save response_with @shipment、:location => shipships_url else flash [:notice] = "Not saved"endendの場合はこれまでのところ取得しています。
これが私が抱えている最後の問題です。
TypeError (can't convert Symbol into Integer):
app/controllers/shipments_controller.rb:24:in `[]'
app/controllers/shipments_controller.rb:24:in `create'
とった。以下の正解で変更を加えた後。コントローラーを次のように修正することができました
@product_shipments = @shipment.product_shipments.build(params[:product_shipments])