2011-04-01 4 views
0

다음 클립 클립 설정이 있습니다. 무슨 일이 일어나는가 나는 proc을 사용하여 다양한 스타일의 크기를 설정하고있다. 그러나 proc은 새로운 호출과 수퍼 호출 중에 호출됩니다. 나는 디버거를 통해 걸 으면서 : photo 매개 변수를 먼저 처리하므로 첨부 파일을 초기화하고 실제 객체 (사진)가 전달 된 매개 변수 (특히 photo.gallery_id)에 의해 초기화되지 않은 스타일 proc를 호출합니다. 그래서 제대로 스타일을 설정하지 않습니다. 심지어 재 처리를 시도하고 도움이되지 않았다. 난 여전히이에 며칠과 행운을 보냈어요. 어떤 도움에 감사드립니다! 것과동적 문서 클립 초기화되기 전에 호출 된 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]}) 

그리고이 올바르게 스타일을 설정하지 않은 그들은 그들이 전무했다 I를. 다음 코드

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을 사용하여 업로드 된 비디오의 미리보기를 만듭니다. 미리보기 이미지로 사용할 프레임을 설정하는 옵션이 있습니다.

자산 업로드에 중첩 된 양식 (멋진 중첩 된 양식)도 사용하고 있습니다. 그래서 내가 한 것은 파일 찾아보기 버튼 앞에 프레임 시간 매개 변수를 넣었습니다. 빌더를 사용하지 않아서 문제가 해결되었습니다.

관련 문제