【问题标题】:uninitialized constant Paperclip::Cropper未初始化的常量 Paperclip::Cropper
【发布时间】:2017-05-26 07:11:15
【问题描述】:

我有一个用于我的回形针样式的自定义处理器:cropper.rb。虽然它没有被调用并返回 NameError (uninitialized constant 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

【问题讨论】:

    标签: ruby-on-rails paperclip


    【解决方案1】:

    好吧,花了一天时间才意识到正确的 lib 子文件夹实际上是 paperclip 而不是 paperclip_processors,尽管 Paperclip Git 确实提到了有效和自动加载。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-11-25
      • 1970-01-01
      • 2018-03-08
      • 1970-01-01
      • 2016-09-08
      • 2011-12-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多