简体   繁体   English

Rails的嵌套属性未更新模型,但参数正确

[英]Rails Nested Attribute Not Updating Model, But Params are Correct

I have a nested form that will not update for whatever reason, and no update is being thrown, and the form params appear correct. 我有一个嵌套的表单,无论出于何种原因都不会更新,并且不会引发任何更新,并且表单参数似乎正确。

The problem is with "medium_prices_attributes" 问题出在“ medium_prices_attributes”

Here's the params: "store"=> {"style_price_list_attributes"=> {"frame_low_cost"=>"4.00", "frame_middle_cost"=>"5.00", "frame_high_cost"=>"6.00", "id"=>"53", "color_styles_avail"=>"true", "frame_avail"=>"true", "stretcher_bars_75"=>"5.00", "stretcher_bars_75_avail"=>"true", "stretcher_bars_1_25"=>"5.00", "stretcher_bars_1_25_avail"=>"false", "stretcher_bars_1_5"=>"5.00", "stretcher_bars_1_5_avail"=>"true", "stretcher_bars_1_75"=>"5.00", "stretcher_bars_1_75_avail"=>"false" }, "shipping_price_list_attributes"=> {"shipping_prices"=>{"minimum_price"=>"5.00", "price_for_print_only"=>"7.00", "price_wrapped"=>"", "price_framed"=>""}, "id"=>"52"}, "medium_prices_attributes"=> {"0"=>{"price_per_sq_ft"=>"10.00", "id"=>"2458"}, "1"=>{"price_per_sq_ft"=>"10.00", "id"=>"2466"}, "2"=>{"price_per_sq_ft"=>"10.00", "id"=>"2467"}, "3"=>{"price_per_sq_ft"=>"99", "id"=>"2457"}, "4"=>{"price_per_sq_ft"=>"99", "id"=>"2459"}, "5"=>{"price_per_sq_ft"=>"111.00", "id"=>"2465"}}, "medium_availabilities_attributes"=> {"0"=>{"id"=>"4005"}, "1"=>{"id"=>"4006"}, "2"=>{"id"=>"4010"}, "3"=>{"id"=>"4009"}, "4"=>{"id"=>"4011"}, "5"=>{"id"=>"4012"}}, "page_attributes"=> {"name"=>"Store22", "headline_text"=>"Store", "id"=>"1124"}, "paypal_email"=>"", "currency"=>"USD", "default_sizes"=>"6x8"}, "page"=>{"online_status"=>"true", "pswd"=>"", "active_billboard_id"=>"", "title"=>"Store", "meta_keywords"=>"", "meta_description"=>"", "slug"=>"store", "script_head"=>"", "script_body"=>""}, "id"=>"123"} 这是参数: "store"=> {"style_price_list_attributes"=> {"frame_low_cost"=>"4.00", "frame_middle_cost"=>"5.00", "frame_high_cost"=>"6.00", "id"=>"53", "color_styles_avail"=>"true", "frame_avail"=>"true", "stretcher_bars_75"=>"5.00", "stretcher_bars_75_avail"=>"true", "stretcher_bars_1_25"=>"5.00", "stretcher_bars_1_25_avail"=>"false", "stretcher_bars_1_5"=>"5.00", "stretcher_bars_1_5_avail"=>"true", "stretcher_bars_1_75"=>"5.00", "stretcher_bars_1_75_avail"=>"false" }, "shipping_price_list_attributes"=> {"shipping_prices"=>{"minimum_price"=>"5.00", "price_for_print_only"=>"7.00", "price_wrapped"=>"", "price_framed"=>""}, "id"=>"52"}, "medium_prices_attributes"=> {"0"=>{"price_per_sq_ft"=>"10.00", "id"=>"2458"}, "1"=>{"price_per_sq_ft"=>"10.00", "id"=>"2466"}, "2"=>{"price_per_sq_ft"=>"10.00", "id"=>"2467"}, "3"=>{"price_per_sq_ft"=>"99", "id"=>"2457"}, "4"=>{"price_per_sq_ft"=>"99", "id"=>"2459"}, "5"=>{"price_per_sq_ft"=>"111.00", "id"=>"2465"}}, "medium_availabilities_attributes"=> {"0"=>{"id"=>"4005"}, "1"=>{"id"=>"4006"}, "2"=>{"id"=>"4010"}, "3"=>{"id"=>"4009"}, "4"=>{"id"=>"4011"}, "5"=>{"id"=>"4012"}}, "page_attributes"=> {"name"=>"Store22", "headline_text"=>"Store", "id"=>"1124"}, "paypal_email"=>"", "currency"=>"USD", "default_sizes"=>"6x8"}, "page"=>{"online_status"=>"true", "pswd"=>"", "active_billboard_id"=>"", "title"=>"Store", "meta_keywords"=>"", "meta_description"=>"", "slug"=>"store", "script_head"=>"", "script_body"=>""}, "id"=>"123"} "store"=> {"style_price_list_attributes"=> {"frame_low_cost"=>"4.00", "frame_middle_cost"=>"5.00", "frame_high_cost"=>"6.00", "id"=>"53", "color_styles_avail"=>"true", "frame_avail"=>"true", "stretcher_bars_75"=>"5.00", "stretcher_bars_75_avail"=>"true", "stretcher_bars_1_25"=>"5.00", "stretcher_bars_1_25_avail"=>"false", "stretcher_bars_1_5"=>"5.00", "stretcher_bars_1_5_avail"=>"true", "stretcher_bars_1_75"=>"5.00", "stretcher_bars_1_75_avail"=>"false" }, "shipping_price_list_attributes"=> {"shipping_prices"=>{"minimum_price"=>"5.00", "price_for_print_only"=>"7.00", "price_wrapped"=>"", "price_framed"=>""}, "id"=>"52"}, "medium_prices_attributes"=> {"0"=>{"price_per_sq_ft"=>"10.00", "id"=>"2458"}, "1"=>{"price_per_sq_ft"=>"10.00", "id"=>"2466"}, "2"=>{"price_per_sq_ft"=>"10.00", "id"=>"2467"}, "3"=>{"price_per_sq_ft"=>"99", "id"=>"2457"}, "4"=>{"price_per_sq_ft"=>"99", "id"=>"2459"}, "5"=>{"price_per_sq_ft"=>"111.00", "id"=>"2465"}}, "medium_availabilities_attributes"=> {"0"=>{"id"=>"4005"}, "1"=>{"id"=>"4006"}, "2"=>{"id"=>"4010"}, "3"=>{"id"=>"4009"}, "4"=>{"id"=>"4011"}, "5"=>{"id"=>"4012"}}, "page_attributes"=> {"name"=>"Store22", "headline_text"=>"Store", "id"=>"1124"}, "paypal_email"=>"", "currency"=>"USD", "default_sizes"=>"6x8"}, "page"=>{"online_status"=>"true", "pswd"=>"", "active_billboard_id"=>"", "title"=>"Store", "meta_keywords"=>"", "meta_description"=>"", "slug"=>"store", "script_head"=>"", "script_body"=>""}, "id"=>"123"}

My store.rb has: 我的store.rb有:

attr_accessible :medium_prices_attributes

has_many :medium_prices

accepts_nested_attributes_for :medium_prices

As you can see in my params, the style_price_list_attributes and shipping_price_list_attributes are there, and they update correctly. 如您在我的参数中所见,style_price_list_attributes和shipping_price_list_attributes在那里,它们可以正确更新。 It's only the medium_prices that have the problem. 只是存在问题的medium_prices。

When I try to update the attributes in the console, I get a "true" response, however the value of that medium_price record doesn't change. 当我尝试在控制台中更新属性时,我得到一个“ true”响应,但是那个medium_price记录的值没有改变。 For example: 例如:

Store.find(163).update_attributes("medium_prices_attributes"=>{"1"=>{"price_per_sq_ft"=>"122.00", "id"=>"2457"}})

(0.2ms)  BEGIN
(0.5ms)  UPDATE "stores" SET "updated_at" = '2013-12-12 15:19:24.042735',         "default_sizes" = '---
- 6x8
' WHERE "stores"."id" = 123
   (0.3ms)  COMMIT
=> true

Thanks! 谢谢!

After much turmoil, it turns out I had 经历了很多动荡之后,事实证明我有

has_many :medium_prices

listed twice on my Store model. 在我的商店模型中列出两次。 The list was pretty long so I didn't catch it at the time. 这份名单很长,所以当时我没听清楚。 Rails doesn't throw an error, so this was hard to track down. Rails不会引发错误,因此很难跟踪。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM