0

データベースに投稿するための追加の属性を取得できない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])
4

2 に答える 2

1

「作成」アクションは、次のように単純にすることができます。

def create
  @shipment = Shipment.new(params[:shipment])

  if @shipment.save
    # success
  else
    # failure
  end
end

ネストされた属性を使用して、新しい出荷レコードを介してshipment_productsを作成する場合。これを行うには、出荷モデルに以下を追加します

class Shipment
  attr_accessible :shipment_products_attributes
  accepts_nested_attributes_for :shipment_products
end

ビューで使用fields_forすることにより、これにより、貨物はこれらを受け入れて、shipment_productsにparams[:shipment][:shipment_products_attributes]渡すことができます。

あなたの新しい行動では、あなたは次のようなことをすることができます

def new
  @shipment = Shipment.new
  # collect the ids of the products you want to create shipment products for
  @shipment.shipment_products.build([{:product_id=> ...},{:product_id=> ...}, ...])
end

その形であなたは次のようなことをすることができます

<%= form_for @shipment, :remote => true do |f|%>
  ...
  ...
  <ul>
    <%= f.fields_for :shipment_products do |sp_f| %>
      <li>
        <%= sp_f.text_field :qty_shipped %>
        <%= sp_f.hidden_field :product_id %>
      </li>
    <% end %>
  </ul>
<% end %>
于 2012-06-28T03:42:31.373 に答える
1

最も簡単な解決策は、次のようなハッシュの配列を生成する必要があることです。

:product_shipments=>[{:product_id=>1, :qty_shipped=>32},{:product_id=>3, :qty_shipped=>23}]

:shipment=>{:product_ids=>[1,3]}2セットのハッシュと。の代わりに:product_shipments=>[:qty_shipped=>[32,23]]

これを行うには、ビューコードを次のように変更します

<%= hidden_field_tag("product_shipments[][product_id]", product.id) %>
<%= product.product_name %><%= text_field_tag("product_shipments[][qty_shipped]")%>

そうすれば、コントローラーのアクションはそのまま機能するはずです。

于 2012-06-28T21:43:17.163 に答える