【问题标题】:Minimagick: undefined method `destroy!' for true:TrueClass auto_orientMinimagick:未定义的方法“销毁!”为真:TrueClass auto_orient
【发布时间】:2014-12-12 18:35:56
【问题描述】:

我上传的图片按预期工作,除非我向 ImageUploader 添加额外的进程 (auto_orient)。代码如下:

class ImageUploader < CarrierWave::Uploader::Base
  include CarrierWave::MiniMagick

  retina!

  if Rails.env.development?
    def store_dir
      "#{Rails.root}/public/uploads/account_#{model.account_id}/product_#{model.product_id}/image_#{model.id}"
    end
  elsif Rails.env.staging? || Rails.env.production?
    def store_dir
      "uploads/account_#{model.account_id}/product_#{model.product_id}/image_#{model.id}"
    end
  end

  def default_url
    "/assets/no_image_500px.png"
  end


  process :auto_orient
  process :resize_and_pad => [500, 500]

  version :thumb_100 do
    process :resize_and_pad => [100, 100]
  end

  version :shopping_cart do
    process :resize_to_fill => [200, 200]
    process :retina_quality => 100
  end

  version :store_page do
    process :resize_to_fill => [336, 336]
    process :retina_quality => 100
  end

  version :product_page do
    process :resize_to_fill => [426, 426]
    process :retina_quality => 100
  end

  def extension_white_list
    %w(jpg jpeg png tiff)
  end

  def auto_orient
    manipulate! do |img|
      img = img.auto_orient
    end
  end

end

Auto_orient 的目的是让用户从他们的手机上传图片,图片是自动定向的。此当前代码适用于移动上传的图像,但是当我尝试从桌面上的网络应用程序上传图像时,我收到:“未定义的方法 `destroy!'为真:TrueClass”。

我查看了documentationfor minimagick 并考虑将 auto_orient 更改为 auto_orient!但后来我被告知 minimagick 没有名为 auto_orient 的方法!为什么用这个方法调用destroy?

【问题讨论】:

  • 检查auto_orient方法返回true还是object。主要是用 !返回真或假。尝试替换操作方法而不是操作!。
  • 显然,对于carrierwave 模块,没有auto_orient 方法(没有!)。看文档,好像是返回了img。
  • @Tommyixi 在你的问题中,你用相反的方式写它。哪个是对的?
  • @PatrickOscity 对不起,我的意思是说没有操纵方法,只有操纵!

标签: ruby-on-rails ruby ruby-on-rails-4 minimagick


【解决方案1】:

它看起来像操纵!在 auto_orient 返回时调用 destroy 方法(最终成为布尔值 (true))。因此,无法在布尔值上调用 delete 的错误。为了解决这个问题,我添加了 ruby​​ 的“tap”方法将图像传递给块,然后立即返回图像:

  def auto_orient
    manipulate! do |img|
      img.tap(&:auto_orient)
    end
  end

【讨论】:

    【解决方案2】:

    也许回答你的问题为时已晚,但我有另一个答案,解决破坏!麻烦您必须返回并在操作块内进行 img

      def auto_orient
        manipulate! do |img|
          img = img.auto_orient
          img
        end
      end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-10
      • 1970-01-01
      • 1970-01-01
      • 2013-08-23
      • 2014-05-27
      • 1970-01-01
      • 1970-01-01
      • 2013-12-26
      相关资源
      最近更新 更多