【问题标题】:rails 3.2 update has_many :throughrails 3.2 更新 has_many :通过
【发布时间】:2012-07-05 04:47:05
【问题描述】:

我非常接近这一点,但赶上了一个小细节。我正在尝试更新 has_many :through 关系。当我提交编辑表单时,我无法提取我想要更新的适当属性。更新发货数量的循环不会使用正确的值更新该字段。如何从 params[:product_shipments] 哈希中仅提取 qty_shipped 属性?

This is the contents of my params[:product_shipments] hash that the update action is working with

"product_shipments"=> {"82"=>{"qty_shipped"=>"234"},
                       "83"=>{"qty_shipped"=>"324"},
                       "84"=>{"qty_shipped"=>"324"}}, 
                       "commit"=>"Update Shipment", "id"=>"250"}

其中包含我更新货件所需的所有信息,因为@shipment.product_shipments 循环将更新限制为仅适用的 shipping_id。我的问题是这是更新操作调用的以下 sql

ProductShipment Load (0.3ms)  SELECT `product_shipments`.* FROM `product_shipments` WHERE `product_shipments`.`shipment_id` = 250
BEGIN
UPDATE `product_shipments` SET `qty_shipped` = 1 WHERE `product_shipments`.`id` = 82
COMMIT
BEGIN
UPDATE `product_shipments` SET `qty_shipped` = 1 WHERE `product_shipments`.`id` = 83
COMMIT
BEGIN
UPDATE `product_shipments` SET `qty_shipped` = 1 WHERE `product_shipments`.`id` = 84
COMMIT

这是产生上述 sql 的更新操作:

def update
  @shipment = Shipment.find(params[:id])
  @shipment.update_attributes(params[:shipment])

  @shipment.product_shipments.each do |shipment|
    shipment.update_attributes(:qty_shipped=> params[:product_shipments])
  end

  respond_with @shipment, :location => shipments_url
end

不推荐使用 rbates nested_forms gem,因为我想弄清楚这一点是为了了解 rails 的工作原理。

<%= hidden_field_tag("product_shipments[][#{product_shipment.id}]") %>
<%= hidden_field_tag("product_shipments[][product_id]", product_shipment.id) %>
<%= text_field_tag "product_shipments[][qty_shipped]", product_shipment.qty_shipped,:class => 'shipment_qty_field'%>&nbsp<%=@product.product_name %>

【问题讨论】:

    标签: ruby ruby-on-rails-3 ruby-on-rails-3.1 has-many-through


    【解决方案1】:
    @shipment.product_shipments.each do |product_shipment|
       product_shipment.update_attributes(:qty_shipped => params[:product_shipments][product_shipment.id][:qty_shipped])
    end
    

    您不必做所有这些,只需使用嵌套表单即可。这是 Rails!

    http://railscasts.com/episodes/196-nested-model-form-part-1

    你的参数应该是这样的

    {:product_shipments => { 79 => { :qty_shipped => 450 }, 80 => { :qty_shipped => 35 } }, :shipment_id => 1 }
    

    要做到这一点,你应该这样命名你的字段

    <input name="product_shipments[79][qty_shipped]" value="450" />
    <input name="product_shipments[80][qty_shipped]" value="35" />
    

    要生成它,

    <% @shipment.product_shipments.each do |product_shipment| %>
      <%= text_field_tag "product_shipments[#{product_shipment.id}][qty_shipped]", product_shipment.qty_shipped || 0 %>
    <% end %>
    

    【讨论】:

    • 得到以下信息:NoMethodError(nil:NilClass 的未定义方法 `[]'):
    • 您应该在表单中设置字段及其名称以解决此问题。 form_for @shipment do |f| @shipment.product_shipments.each do |product_shipment| text_field_tag "product_shipments[#{product_shipment.id}][qty_shipped]", product_shipment.qty_shipped end end 当您在 Rails 中使用嵌套表单时,所有这些都会自动处理。
    • 不能使用 Ryan 的 gem。我已经编辑了问题以包括 params 哈希的内容以及对我发现的内容的一些分析。似乎不需要传递 shipping_id ,因为这就是 shipping.update_attributes 已经包含的内容。我已经尝试过了,但仍然得到与我的第一条评论相同的错误。
    • 使用的不是 shipping.id。这是product_shipment.id,我稍后会编辑答案,以免混淆。您确实需要在字段中使用此 ID,以便您可以确定哪个值属于哪个 product_shipment,即使所有 product_shipments 都属于发货。
    • NoMethodError (nil:NilClass 的未定义方法 `[]'
    猜你喜欢
    • 2012-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-19
    相关资源
    最近更新 更多