2009-09-01 54 views
0

我在使用attachment_fu的Image型號上有has_manyaccepts_nested_attributes_for屬性模型。使用以下代碼更新屬性將導致每個圖像(和每個圖像縮略圖)的數據庫UPDATE,而不管它是否有更改。爲什麼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)爲什麼它應該這樣認爲。

+0

從rails控制檯運行以下命令會產生相同的結果,圖像及其縮略圖會更新,但只會設置size和updated_at字段:i = Image.last; i.caption = i.caption; i.save; – 2009-09-01 17:49:34

回答

0

嗯,我應該已經搜查計算器有點更徹底地張貼在此之前,作爲Matchu已經分叉attachment_fu和問題913555解決了這個問題了。用這個替換technoweenie的一個解決了我所有的問題。

0

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 %> 
+0

fields_for的第二個參數需要實例爲'image'變量中的.each循環提供的字段。它創建了正確的標記,但如果我在表單中沒有更改(僅用於更改標題和刪除)並提交它,它會更新每個設置updated_at和size列的單個圖像。 – 2009-09-01 17:38:21