2011-04-01 41 views
0

我有以下回形針設置。會發生什麼是我正在使用proc來設置各種樣式的大小。但是,proc會在新的超級調用期間被調用。我走過調試器,看起來它首先處理:photo參數,所以它初始化附件,並調用樣式過程,實際對象(照片)未被傳入的參數初始化(特別是photo.gallery_id所以它不設置樣式正確。我甚至嘗試再加工,並沒有幫助。我已經花了這幾天,仍然沒有運氣。任何幫助表示讚賞!對象初始化之前調用的回形針動態Proc樣式

class Photo < ActiveRecord::Base 
    has_and_belongs_to_many :staffs 
has_attached_file :photo, 
        :storage => :s3, 
        :s3_credentials => "#{RAILS_ROOT}/config/s3.yml", 
        :path => "/assets/:id/:class/:style/:image_name.:extension", 
        :url => "/assets/:id/:class/:style/:image_name.:extension", 
        :styles => Proc.new { |clip| clip.instance.attachment_styles} 

    def attachment_styles 
    if self.gallery.nil? 
     { :original => { 
         :processors => [:watermark], 
         :geometry =>"600x800!", 
         :watermark_path => ':rails_root/public/images/watermark.png', 
         :position => 'SouthEast'}, 
      :thumbnail => { 
         :processors => [:watermark], 
         :geometry => "200x300!", 
         :watermark_path => ':rails_root/public/images/watermark.png', 
         :position => 'SouthEast'} 
     } 
    elsif self.photo.styles.empty? 
     gallery_type = GalleryType.find_by_id(self.gallery_id) 
     { :original => { 
         :processors => [:watermark], 
         :geometry =>"#{gallery_type.width_max}x#{gallery_type.height_max}!", 
         :watermark_path => ':rails_root/public/images/watermark.png', 
         :position => 'SouthEast'}, 
      :thumbnail => { 
         :processors => [:watermark], 
         :geometry => "#{gallery_type.width_min}x#{gallery_type.height_min}!", 
         :watermark_path => ':rails_root/public/images/watermark.png', 
         :position => 'SouthEast'} 
     } 
    else 
     self.photo.styles 
    end 
    end 


    def reprocess_att 
    self.photo.reprocess! 
    end 

    def initialize(galleryid, params = {}) 
    begin 
     param.merge!({"gallery_id" => galleryid.to_s}) 
     super(params) 
    rescue => e 
     puts e.message() 
    end 
    end 

回答

1

從什麼我可以看到,參數的順序很重要,我有:

attachments.build(:upload => File.new(File.dirname(__FILE__) + '/../fixtures/test-image.jpg'),                                                
:styles => {:small => ['100x100#', :jpg], :medium => ['250x250', :jpg]}) 

而這不是正確設置樣式,他們是零。它改爲:

attachments.build(:styles => {:small => ['100x100#', :jpg], :medium => ['250x250', :jpg]},                                                
:upload => File.new(File.dirname(__FILE__) + '/../fixtures/test-image.jpg')) 

然後代碼:

:styles => lambda { |a| a.instance.styles || {} } 

完美。希望這可以幫助。

0

感謝您的回答!

我一直在爲此奮鬥了幾個星期。我使用FFMPEG的Paperclip製作上傳視頻的縮略圖。我可以選擇設置要用作縮略圖的幀。

我還爲我的資產上傳使用了嵌套表單(真棒嵌套表單)。 所以我所做的是在文件瀏覽按鈕之前放置了幀時間參數。這解決了我的問題,因爲我不使用構建器。