Carrierwave,MiniMagick - NoMethodError:nil的未定义方法`size':NilClass

时间:2013-12-09 18:25:16

标签: ruby-on-rails ruby ruby-on-rails-3 carrierwave minimagick

在暂存时,我在通过carrierwave和minimagick上传和调整图像大小时遇到​​以下错误。在当地一切正常。

carrierwave(0.9.0) mini_magick(3.7.0)

irb(main):003:0> PicturePost.create(remote_content_url: 'http://www.imagpress.com/img/slider/slider_1.jpg')
NoMethodError: undefined method `size' for nil:NilClass
from /home/deploy/apps/staging/blog/shared/bundle/ruby/1.9.1/gems/mini_magick-3.7.0/lib/mini_magick.rb:24:in `choose_processor'
from /home/deploy/apps/staging/blog/shared/bundle/ruby/1.9.1/gems/mini_magick-3.7.0/lib/mini_magick.rb:64:in `mogrify?'
from /home/deploy/apps/staging/blog/shared/bundle/ruby/1.9.1/gems/mini_magick-3.7.0/lib/mini_magick/image.rb:360:in `run_command'
from /home/deploy/apps/staging/blog/shared/bundle/ruby/1.9.1/gems/mini_magick-3.7.0/lib/mini_magick/image.rb:171:in `valid?'
from /home/deploy/apps/staging/blog/shared/bundle/ruby/1.9.1/gems/mini_magick-3.7.0/lib/mini_magick/image.rb:140:in `create'
from /home/deploy/apps/staging/blog/shared/bundle/ruby/1.9.1/gems/mini_magick-3.7.0/lib/mini_magick/image.rb:48:in `read'
from /home/deploy/apps/staging/blog/shared/bundle/ruby/1.9.1/gems/mini_magick-3.7.0/lib/mini_magick/image.rb:111:in `block in open'
from /home/deploy/apps/staging/blog/shared/bundle/ruby/1.9.1/gems/mini_magick-3.7.0/lib/mini_magick/image.rb:110:in `open'
from /home/deploy/apps/staging/blog/shared/bundle/ruby/1.9.1/gems/mini_magick-3.7.0/lib/mini_magick/image.rb:110:in `open'
from /home/deploy/apps/staging/blog/shared/bundle/ruby/1.9.1/gems/carrierwave-0.9.0/lib/carrierwave/processing/mini_magick.rb:260:in `manipulate!'
from /home/deploy/apps/staging/blog/shared/bundle/ruby/1.9.1/gems/carrierwave-0.9.0/lib/carrierwave/processing/mini_magick.rb:176:in `resize_to_fill'
from /home/deploy/apps/staging/blog/shared/bundle/ruby/1.9.1/gems/carrierwave-0.9.0/lib/carrierwave/uploader/processing.rb:85:in `block in process!'
from /home/deploy/apps/staging/blog/shared/bundle/ruby/1.9.1/gems/carrierwave-0.9.0/lib/carrierwave/uploader/processing.rb:81:in `each'
from /home/deploy/apps/staging/blog/shared/bundle/ruby/1.9.1/gems/carrierwave-0.9.0/lib/carrierwave/uploader/processing.rb:81:in `process!'
from /home/deploy/apps/staging/blog/shared/bundle/ruby/1.9.1/gems/carrierwave-0.9.0/lib/carrierwave/uploader/callbacks.rb:18:in `block in with_callbacks'
from /home/deploy/apps/staging/blog/shared/bundle/ruby/1.9.1/gems/carrierwave-0.9.0/lib/carrierwave/uploader/callbacks.rb:18:in `each'
9 levels...
from /home/deploy/apps/staging/blog/shared/bundle/ruby/1.9.1/gems/carrierwave-0.9.0/lib/carrierwave/uploader/download.rb:65:in `download!'
from /home/deploy/apps/staging/blog/shared/bundle/ruby/1.9.1/gems/carrierwave-0.9.0/lib/carrierwave/mount.rb:353:in `remote_url='
from /home/deploy/apps/staging/blog/shared/bundle/ruby/1.9.1/gems/carrierwave-0.9.0/lib/carrierwave/mount.rb:203:in `remote_content_url='
from /home/deploy/apps/staging/blog/shared/bundle/ruby/1.9.1/gems/carrierwave-0.9.0/lib/carrierwave/orm/activerecord.rb:44:in `remote_content_url='
from /home/deploy/apps/staging/blog/shared/bundle/ruby/1.9.1/gems/activerecord-3.2.15/lib/active_record/attribute_assignment.rb:85:in `block in assign_attributes'
from /home/deploy/apps/staging/blog/shared/bundle/ruby/1.9.1/gems/activerecord-3.2.15/lib/active_record/attribute_assignment.rb:78:in `each'
from /home/deploy/apps/staging/blog/shared/bundle/ruby/1.9.1/gems/activerecord-3.2.15/lib/active_record/attribute_assignment.rb:78:in `assign_attributes'
from /home/deploy/apps/staging/blog/shared/bundle/ruby/1.9.1/gems/activerecord-3.2.15/lib/active_record/base.rb:498:in `initialize'
from /home/deploy/apps/staging/blog/shared/bundle/ruby/1.9.1/gems/activerecord-3.2.15/lib/active_record/persistence.rb:44:in `new'
from /home/deploy/apps/staging/blog/shared/bundle/ruby/1.9.1/gems/activerecord-3.2.15/lib/active_record/persistence.rb:44:in `create'
from (irb):3
from /home/deploy/apps/staging/blog/shared/bundle/ruby/1.9.1/gems/railties-3.2.15/lib/rails/commands/console.rb:47:in `start'
from /home/deploy/apps/staging/blog/shared/bundle/ruby/1.9.1/gems/railties-3.2.15/lib/rails/commands/console.rb:8:in `start'
from /home/deploy/apps/staging/blog/shared/bundle/ruby/1.9.1/gems/railties-3.2.15/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'

无法弄清楚为什么会这样。

7 个答案:

答案 0 :(得分:54)

忘了在我的新服务器上安装imagemagick。 这解决了我的问题

sudo apt-get install imagemagick

答案 1 :(得分:6)

当MiniMagick查找命令mogrifygm并且找不到任何一个命令时,会发生此错误。

如另一个答案中所述,您需要确保使用

等命令安装ImageMagick或GraphicsMagick
sudo apt-get install imagemagick

但是,如果ImageMagick安装在非标准位置并且在Rails进程的路径上不可用,则也会出现此问题。例如,如果从源安装到/usr/local/bin,则该位置可能不在路径上。在这种情况下,为了确保您的路径设置正确,请将此行添加到application.rbenvironments/production.rb或其他适当位置:

ENV['PATH'] += File::PATH_SEPARATOR + '/usr/local/bin'

请注意,您的路径可能会有所不同,具体取决于应用程序的启动方式,例如:从命令行到系统初始化文件,所以添加这样的行对于确保MiniMagick在所有情况下都能正常工作非常重要。

在Linux下,您可以通过查找pid然后查看/proc/<pid>/environ来检查正在运行的Rails进程的路径。

答案 2 :(得分:2)

看起来像mini_magick 3.7.0的问题。看看这个issue

答案 3 :(得分:2)

Homebrew&amp;以优胜美地为中心答案:

http://thornelabs.net/2014/12/08/libpng-not-symlinking-when-installing-imagemagick-on-os-x-yosemite.html

当然,您需要重新安装ImageMagick,但如果正在运行

class VerticallyCenteredTextView: UITextView {
    override var contentSize: CGSize {
        didSet {
            var topCorrection = (bounds.size.height - contentSize.height * zoomScale) / 2.0
            topCorrection = max(0, topCorrection)
            contentInset = UIEdgeInsets(top: topCorrection, left: 0, bottom: 0, right: 0)
        }
    }
}

给您一个错误:

brew install imagemagick

首先,检查您的用户名是否具有

的权限
Error: The `brew link` step did not complete successfully
The formula built, but is not symlinked into /usr/local
Could not symlink share/man/man5/png.5
/usr/local/share/man/man5 is not writable.
...

如果不这样做,则需要运行

ls -al /usr/local/share/man

希望这可以节省一些时间。

-B

答案 4 :(得分:1)

如果您正在使用yum包管理器(就像我使用的是AWS Opsworks)。你可以做到

yum install ImageMagick

注意:ImageMagick拼写为camelcase

答案 5 :(得分:0)

如果您使用某些IDE,可能只需重新启动IDE即可解决问题。我花了很多时间试图找到解决这个问题的方法。这个简单的操作对我有用。

答案 6 :(得分:0)

对于正在使用Rails并使用mini_magick的其他人,请在gemfile中将版本更改为3.5,因为使用mini_magick进行图像上传时会破坏3.6,而对3.7版本无效。