Rails 3.2 has_many通过表单提交 [英] Rails 3.2 has_many through form submission
问题描述
我有一个has_many:through表单,在这里我无法获得额外的属性来发布到数据库.我在某个地方弄错了参数名称.我可以发布外键,但是我想要在连接表中跟踪另一个属性.请记住,这是一种100%基于ajax的形式.这就是我所知道的
I have a has_many :through form where I can't get an extra attribute to post to the database. I'm fouling up the parameter name somewhere. I can get the foreign keys to post but I have another attribute that I'm trying to track in the join table. Keep in mind this is a 100% ajax based form. Here's what I know
在研究了类似的问题之后,我知道我应该构建表单属性,但是由于某种原因,我发现的代码无法正常工作.这里有一些资源.
After researching similar issues, I understand I'm supposed to build the form attributes, but the code I've found doesn't work for some reason. Here are some resources.
http://railsforum.com/viewtopic.php?id=20203
我不了解的是,将product_ids附加到了rails中.这些值发布.为什么很难将quantity_shipped属性附加到该数组?
What I don't understand is that attaching the product_ids is built into rails. Those values post. Why is it hard to attach the quantity_shipped attribute to that array?
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数量生成另一个数组.
This partial is looped through a few times displaying all the products from a certain vendor. It generates an array for the product_ids and another for the product_shipments quantities.
_product_shipments.html.erb.
_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数据
This is the relevant POST data when the form is submitted
"product_ids"=>["1", "3"]}, "product_shipments"=>{"qty_shipped"=>["32", "23"]}
这是发送到数据库的sql
This is sql that is sent to the database
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)
这是我的控制器中的动作
Here is the action in my controller
def create
@shipment = Shipment.new(params[:shipment])
@product_shipments = @shipment.product_shipments.build(params[:product_shipments])
[:qty_shipped])<-不正确,但这是我到目前为止得到的 如果@ shipment.save response_with @shipment,:location =>发货地址 别的 flash [:notice] =未保存" 结尾 结束
[:qty_shipped]) <- not correct, but it's what i got so far if @shipment.save respond_with @shipment, :location => shipments_url else flash[:notice]= "Not saved" end end
这是我最近遇到的问题.
Here's the last issue I'm having.
TypeError (can't convert Symbol into Integer):
app/controllers/shipments_controller.rb:24:in `[]'
app/controllers/shipments_controller.rb:24:in `create'
知道了.进行以下正确答案后进行更改.我能够将控制器更正为以下
GOT IT. After making the changes with the correct answer below. I was able to correct the controller to the following
@product_shipments = @shipment.product_shipments.build(params[:product_shipments])
推荐答案
最简单的解决方案是您需要生成一个看起来像哈希的数组
The simplest solution is that you need to generate an array of hashes that looks like
: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]]
.
为此,请将您的查看代码更改为
To do this, change your view code to
<%= hidden_field_tag("product_shipments[][product_id]", product.id) %>
<%= product.product_name %><%= text_field_tag("product_shipments[][qty_shipped]")%>
然后,您的控制器操作应按原样工作.
Then your controller action should work as is.
这篇关于Rails 3.2 has_many通过表单提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!