为什么attachment_fu每次更新附件而不只是改变的呢?
-
20-09-2019 - |
题
我有一个属性模型与在其上使用一个has_many
图像模型accepts_nested_attributes_for
和attachment_fu
。使用下面的代码的结果为每一个图像数据库更新(以及每个图像的缩略图)不管是否有变化或不更新属性。
properties_controller.rb
def update
@property.update_attributes params[:property]
redirect_to edit_property_path(@property)
end
_form.html.erb
<% form_for @property do |property| %>
...
<ul id='image-admin'>
<% @property.images.each do |image| %>
<li>
<%= image_tag image.public_filename(:front), :alt=> h(image.caption), :size => "218x160" %>
<% property.fields_for :images, image do |img| %>
<%= img.hidden_field :ordering, :class => 'order' %>
<%= img.text_field :caption %>
<span class='img_remove'>
Remove ? <%= img.check_box '_delete' %>
</span>
<% end %>
</li>
<% end %>
</ul>
...
<% end %>
脚本/服务器的输出
SQL (8.7ms) COMMIT
SQL (0.1ms) BEGIN
Image Update (0.3ms) UPDATE `images` SET `updated_at` = '2009-09-01 15:17:19', `size` = 103402 WHERE `id` = 350
Image Load (0.5ms) SELECT * FROM `images` WHERE (`images`.`thumbnail` = 'small' AND `images`.`parent_id` = 350) ORDER BY ordering ASC LIMIT 1
Image Update (0.3ms) UPDATE `images` SET `updated_at` = '2009-09-01 15:17:19', `size` = 60535 WHERE `id` = 352
Image Load (0.5ms) SELECT * FROM `images` WHERE (`images`.`thumbnail` = 'front' AND `images`.`parent_id` = 350) ORDER BY ordering ASC LIMIT 1
Image Update (0.3ms) UPDATE `images` SET `updated_at` = '2009-09-01 15:17:19', `size` = 39888 WHERE `id` = 353
Image Load (0.4ms) SELECT * FROM `images` WHERE (`images`.`thumbnail` = 'thumb' AND `images`.`parent_id` = 350) ORDER BY ordering ASC LIMIT 1
Image Update (0.3ms) UPDATE `images` SET `updated_at` = '2009-09-01 15:17:19', `size` = 3510 WHERE `id` = 351
SQL (0.9ms) COMMIT
SQL (0.1ms) BEGIN
Image Update (0.3ms) UPDATE `images` SET `updated_at` = '2009-09-01 15:17:19', `size` = 100387 WHERE `id` = 338
Image Load (0.4ms) SELECT * FROM `images` WHERE (`images`.`thumbnail` = 'small' AND `images`.`parent_id` = 338) ORDER BY ordering ASC LIMIT 1
Image Update (0.3ms) UPDATE `images` SET `updated_at` = '2009-09-01 15:17:19', `size` = 58212 WHERE `id` = 340
Image Load (0.4ms) SELECT * FROM `images` WHERE (`images`.`thumbnail` = 'front' AND `images`.`parent_id` = 338) ORDER BY ordering ASC LIMIT 1
Image Update (0.8ms) UPDATE `images` SET `updated_at` = '2009-09-01 15:17:20', `size` = 38101 WHERE `id` = 341
Image Load (0.4ms) SELECT * FROM `images` WHERE (`images`.`thumbnail` = 'thumb' AND `images`.`parent_id` = 338) ORDER BY ordering ASC LIMIT 1
Image Update (0.3ms) UPDATE `images` SET `updated_at` = '2009-09-01 15:17:20', `size` = 3241 WHERE `id` = 339
SQL (0.8ms) COMMIT
任何想法,为什么(我怎么能阻止)从这样attachment_fu
?它看起来像它认为,大小属性发生变化,但我看不到任何理由(在我的代码,或在attachment_fu
)为什么要这样认为。
其他提示
fields_for
重复用于在关联每个图像封闭字段。但是你有一个外循环,重复fields_for
在联想每一个形象。当你提交你发送更多的更新速度比你想被发送。
尝试这种情况:
<% form_for @property do |property| %>
...
<ul id='image-admin'>
<% property.fields_for :images, image do |img| %>
<li>
<%= image_tag img.object.public_filename(:front), :alt=> h(img.object.caption), :size => "218x160" %>
<%= img.hidden_field :ordering, :class => 'order' %>
<%= img.text_field :caption %>
<span class='img_remove'>
Remove ? <%= img.check_box '_delete' %>
</span>
</li>
<% end %>
</ul>
...
<% end %>
不隶属于 StackOverflow