0

次のペーパークリップのセットアップがあります。何が起こるかというと、さまざまなスタイルのサイズを設定するために proc を使用しているということです。ただし、proc は new で呼び出され、super 呼び出し中に呼び出されます。デバッガーを調べたところ、最初に :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
4

2 に答える 2

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 || {} }

完璧に機能しました。お役に立てれば。

于 2011-12-29T06:44:15.227 に答える