未初始化的常量Paperclip :: Cropper

时间:2017-01-11 13:08:44

标签: ruby-on-rails paperclip

我的Paperclip样式有一个自定义处理器:cropper.rb。虽然没有调用它并返回 NameError(未初始化的常量Paperclip :: Cropper)错误。

这里已经讨论过:Rails3 and Paperclip但不久前。那是关于Rails 3的。

我在Rails 5下(从Rails 4.x更新)

Profilepic.rb

class Profilepic < ApplicationRecord

  belongs_to :professionnels

  has_attached_file :image, styles: { big: "1200x1200", medium: "400x400", small: "250x250"}
  validates_attachment :image, content_type: { content_type: ["image/jpeg", "image/gif", "image/png"] }, size: {less_than: 10.megabytes}

  has_attached_file :finalimage, styles: { medium: "500x500", small: "200x200"}, whiny: false, use_timestamp: false, processors: [:cropper]

  attr_accessor :crop_x, :crop_y, :crop_w, :crop_h

end

LIB / paperclip_processors / cropper.rb

module Paperclip
  class CustomCropper < Thumbnail
    def initialize(file, options = {}, attachment = nil)
      super
      if target.crop_w && target.crop_x
        @current_geometry.width  = target.crop_w
        @current_geometry.height = target.crop_h
      end
    end

    def target
      @attachment.instance
    end

    def transformation_command
      # call crop processing only if user inputs are there
      if target.crop_w && target.crop_x
        crop_command = [
            "-crop",
            "#{target.crop_w}x" \
            "#{target.crop_h}+" \
            "#{target.crop_x}+" \
            "#{target.crop_y}",
            "+repage"
        ]
        crop_command + super
      else
        super
      end
    end

  end
end

1 个答案:

答案 0 :(得分:0)

确定花了一天的时间来实现正确的lib子文件夹实际上是paperclip而不是paperclip_processors,尽管Paperclip Git确实提到它们都是有效且自动加载的。