我有一个has_many:通过表单,我无法获得额外的属性来发布到数据库。我在某个地方搞砸了参数名称。我可以让外键发布,但我有另一个属性,我试图在连接表中跟踪。请记住,这是一个100%基于ajax的形式。这就是我所知道的
编辑: 在研究了类似的问题之后,我理解我应该构建表单属性,但是我发现的代码由于某种原因不起作用。这是一些资源。
http://railsforum.com/viewtopic.php?id=20203
Rails 3, nested multi-level forms and has_many through
我不明白附加product_ids是内置到rails中的。那些价值观。为什么将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])&lt; - 不正确,但这是我到目前为止所得到的 if @ shipment.save respond_with @shipment,:location =&gt; shipments_url 其他 flash [:notice] =“未保存” 结束 端
这是我遇到的最后一个问题。
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 :(得分: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]
并将其传递到其货运产品。
在您的新动作中,您可以执行类似
的操作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 %>
答案 1 :(得分: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]")%>
然后您的控制器操作应该按原样运行。