在对象初始化之前调用的Paperclip动态Proc样式

我有以下回形针设置。会发生什么是我使用proc设置各种样式的大小。但是,在新的和超级调用期间调用proc。我走过调试器,它似乎首先处理:photo参数,所以它初始化附件并调用样式proc,此时实际对象(Photo)尚未被传入的参数(特别是photo.gallery_id)初始化因此它没有正确设置样式。我甚至尝试过重新处理它没有帮助。我已经花了几天时间,但仍然没有运气。任何帮助表示赞赏!
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
    
已邀请:
从我所看到的,参数的顺序很重要。我有:
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 || {} }
工作得很好。希望这可以帮助。     
谢谢你的回答! 我已经和它斗争了几个星期了。我正在使用Paperclip和FFMPEG制作上传视频的缩略图。我可以选择设置要用作缩略图的帧。 我也使用嵌套表单(很棒的嵌套表单)进行资产上传。所以我所做的是将帧时间参数放在文件浏览按钮之前。这解决了我的问题,因为我没有使用构建器。     

要回复问题请先登录注册