2012-06-26 162 views
0

我有一個has_many:通過表單我無法獲得額外的屬性發布到數據庫。我弄錯了某處的參數名稱。我可以獲得外鍵發佈,但我有另一個屬性,我試圖追蹤連接表中。請記住,這是一個100%基於ajax的表單。這是我所知道的Rails 3.2 has_many通過表單提交

編輯: 經過研究類似的問題,我明白我應該建立表單屬性,但我發現的代碼由於某種原因不起作用。這裏有一些資源。

http://railsforum.com/viewtopic.php?id=20203

Rails 3, nested multi-level forms and has_many through

我不明白的是,安裝在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"]} 

這是發送到數據庫

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) 

這裏SQL是在我的控制器的動作

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

@product_shipments = @shipment.product_shipments.build(params[:product_shipments]) 

[:qty_shipped ])< - 不正確的,但是這是我走到這一步,如果 @ shipment.save respond_with @shipment,:位置=> shipments_url 其他 閃光燈[:聲明] = 「不保存」 結束 結束

這是我遇到的最後一個問題。

TypeError (can't convert Symbol into Integer): 
    app/controllers/shipments_controller.rb:24:in `[]' 
    app/controllers/shipments_controller.rb:24:in `create' 

GOT IT。使用下面的正確答案進行更改後。我能控制糾正以下

@product_shipments = @shipment.product_shipments.build(params[:product_shipments]) 
+0

你的意思是寫'text_field_tag(「shipment_products [qty_shipped] []」)'? – cdesrosiers

+0

它似乎沒有區別。它仍然輸入一個空值 – ctilley79

+0

你的模型是否有'has_many:shipment_products'和'accep_nested_attributes_for:shipment_products'?你爲什麼不用'fields_for'?你知道'simple_form'還是'formtastic'?如果您想要動態添加貨件,請查看[cocoon](https://github.com/nathanvda/cocoon)gem。 – nathanvda

回答

1

最簡單的解決方案的形式是,你需要生成的數組哈希看起來像

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

而不是兩組哈希:shipment=>{:product_ids=>[1,3]}: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]")%> 

那麼你的控制器動作將正常運行是。

+0

好消息是哈希現在看起來不錯。控制器操作需要調整。我在回答結束時發佈了錯誤。 – ctilley79

+0

這解決了它。 @product_shipments = @ shipment.product_shipments.build(params [:product_shipments]) – ctilley79

+0

讓雅爲那50個代表工作:) – ctilley79

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,這將允許裝運接受params[:shipment][:shipment_products_attributes]和傳遞這些在其shipment_products。

在新的動作,你可以不喜歡

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 %> 
+0

在您的回答中,新動作只有在知道您要分配的產品數量時纔有效。如果你不知道怎麼辦?部分產品通過幾次循環生成所有可用於某個供應商的產品,因此產品數量不是有限的。 partial中的shipping_products必須是數組 – ctilley79

+0

只是另一個快速註釋。 shipment_id和product_id正確地發佈到數據庫。這是正在丟失的數量。 – ctilley79

+0

你究竟如何循環部分? – cdesrosiers