2013-07-30 74 views
2

代碼

在我的圖像模型:重命名圖像 - 回形針

has_attached_file :pic 
before_post_process :rename_pic 
before_save ->{ p 'before_save ----------------' } 
after_post_process ->{ p 'after_post_process --------------' } 

    def rename_pic 
    p 'en imagen' 
    p self 
    p 'en imagen' 
    end 

在服務,有很多圖片:

# don't use accepts_nested_attributes_for 
    before_save :create_images 
    attr_accessor :images_attributes 

    def create_images 
    # images_attributes example value: { "0"=> {img_attrs}, "1" => {img_attrs1} } 
    images_attributes.all? do |k, image_attrs| 
     if image_attrs.delete(:_destroy) == "false" 
     p 'asd' 
     image = Image.new image_attrs.merge(service_id: id) 
     p image.service 
     p image.service_id 
     image.save 
     end 
    end 
    end 

這是輸出我得到:

"asd" 
"en imagen" 
#<Image id: nil, service_id: nil, pic_file_name: "Screen_Shot_2013-04-07_at_5.18.03_PM.png", pic_content_type: "image/png", pic_file_size: 16041, pic_updated_at: "2013-07-30 22:58:46", created_at: nil, updated_at: nil, user_id: nil> 
"en imagen" 
G"after_post_process --------------" 
#<Service id: 427, event_id: nil, min_capacity: nil, max_capacity: nil, price: #<BigDecimal:7fb6e9d73d48,'0.0',9(18)>, image_path: nil, name: "Super Franks", desc: "zxc", created_at: "2013-05-12 19:01:54", updated_at: "2013-07-30 19:32:48", address: "pasadena", longitude: 77.225, latitude: 28.6353, gmaps: true, city: "san francisco", state: "california", country_id: "472", tags: "Banquet", created_by: 22, avg_rating: #<BigDecimal:7fb6efdbcf10,'0.0',9(18)>, views: 27, zip_code: "", address2: "", price_unit: "", category_id: 3, featured: true, publish: true, slug: "banquet-super-franks", discount: nil, currency_code: "USD", video_url: "http://www.youtube.com/watch?v=A3pIrBZQJvE", short_description: ""> 
427 
"before_save ----------------" 

問題

當調用

image = Image.new image_attrs.merge(service_id: id) 

回形針似乎開始處理,然後設置的service_id。

所以當我嘗試使用service裏面rename_pic服務是nil

有關如何處理此問題的任何想法?

回答

2

這解決了我的問題,我改變了:

before_post_process :rename_pic 

到:

before_create :rename_pic 

,這是rename_pic,備案:

def rename_pic 
    extension = File.extname(pic_file_name).downcase 
    self.pic.instance_write :file_name, 
    "#{service.hyphenated_for_seo}#{extension}" 
end 

其中servicehas_manyimages,和imagebelongs_toservice

0

要當心與@juanpastas的修復,因爲如果你改變before_post_processbefore_create,只會當你創建你的形象運行,不是當你更新。有回調仍然在更新運行,這樣做:

class YourImage 
    has_attached_file :pic 

    # use both callbacks 
    before_create      :rename_pic 
    before_post_process    :rename_pic 

    def rename_pic 
    # assotiated_object is the association used to get pic_file_name 
    return if self.assotiated_object.nil? 

    extension = File.extname(pic_file_name).downcase 
    self.pic.instance_write :file_name, 
     "#{service.hyphenated_for_seo}#{extension}" 
    end 
end 
+0

嗯,我不重複,這希伯,爲什麼before_save沒有更新過工作時運行的回調? – juanpastas

+0

@juanpastas在保存附件時,根本不會調用回調'before_save'。所以你必須使用'before_create' –

+0

好吧,謝謝,它似乎不一致before_save沒有被調用時,雖然保存附件,你知道原因嗎? – juanpastas