0

我正在尝试使用 Carrierwave 在 Sinatra 中创建一个简单的图像上传器类。我使用了与此几乎相同的代码并在 Gemfile 中指定了相同的行,但是在运行 $bundle install 并且所有安装都没有问题之后,我从以下代码中得到一个 LoadError:

宝石文件:

source 'https://rubygems.org'

ruby '1.9.3'

gem 'sinatra'
gem 'sinatra-contrib'
gem 'rack'
gem 'thin'
gem "mongo_mapper"
gem 'bson_ext'

# Image uploading to S3
gem "fog", "~> 1.3.1"
gem 'carrierwave'
gem 'rmagick', '2.13.2', :git=>'http://github.com/rmagick/rmagick.git', :require=>'RMagick'

CarrierWave 配置文件:

# Configure Carrierwave Uploads to Amazon S3
CarrierWave.configure do |config|
  config.fog_credentials = {
    :provider               => 'AWS',                        # required
    :aws_access_key_id      => '(never mind about this)',                        # required
    :aws_secret_access_key  => '(or this)'                        # required
    #:region                 => 'eu-west-1',                  # optional, defaults to 'us-east-1'
    #:host                   => 's3.example.com',             # optional, defaults to nil
    #:endpoint               => 'https://s3.example.com:8080' # optional, defaults to nil
  }
  config.fog_directory  = 'penumbra-images'                     # required
  config.fog_attributes = {'Cache-Control'=>'max-age=315576000'}  # optional, defaults to {}

end

ImageUploader 类定义:

class ImageUploader < CarrierWave::Uploader::Base
  include CarrierWave::RMagick
  process :resize_to_fit => [1024, 1024]
  storage :fog
end

我的结果:

$ ruby app.rb
CarrierWave::Uploader::Base
/Users/duncanmalashock/.rvm/rubies/ruby-1.9.3-p448/lib/ruby/site_ruby/1.9.1/rubygems/core_ext/kernel_require.rb:124:in `require': cannot load such file -- RMagick (LoadError)
    from /Users/duncanmalashock/.rvm/rubies/ruby-1.9.3-p448/lib/ruby/site_ruby/1.9.1/rubygems/core_ext/kernel_require.rb:124:in `require'
    from /Users/duncanmalashock/.rvm/gems/ruby-1.9.3-p448/gems/carrierwave-0.9.0/lib/carrierwave/processing/rmagick.rb:67:in `rescue in block in <module:RMagick>'
    from /Users/duncanmalashock/.rvm/gems/ruby-1.9.3-p448/gems/carrierwave-0.9.0/lib/carrierwave/processing/rmagick.rb:64:in `block in <module:RMagick>'
    from /Users/duncanmalashock/.rvm/gems/ruby-1.9.3-p448/gems/activesupport-3.2.14/lib/active_support/concern.rb:121:in `class_eval'
    from /Users/duncanmalashock/.rvm/gems/ruby-1.9.3-p448/gems/activesupport-3.2.14/lib/active_support/concern.rb:121:in `append_features'
    from /Users/duncanmalashock/Generator/init/uploader.rb:2:in `include'
    from /Users/duncanmalashock/Generator/init/uploader.rb:2:in `<class:ImageUploader>'
    from /Users/duncanmalashock/Generator/init/uploader.rb:1:in `<top (required)>'
    from /Users/duncanmalashock/.rvm/rubies/ruby-1.9.3-p448/lib/ruby/site_ruby/1.9.1/rubygems/core_ext/kernel_require.rb:124:in `require'
    from /Users/duncanmalashock/.rvm/rubies/ruby-1.9.3-p448/lib/ruby/site_ruby/1.9.1/rubygems/core_ext/kernel_require.rb:124:in `require'
    from app.rb:7:in `<main>'

任何人都可以帮忙吗?非常感谢。

4

1 回答 1

2

在您Gemfile指定了 RMagick 的 git 版本。Bundler 需要一些特殊的步骤来将这些库添加到 Ruby 的加载路径中,但是在没有 Bundler 的情况下运行时它们不可用,因为它们不是普通的 gem。当您在没有使用 Bunder 的情况下运行应用程序时ruby app.rb,Bundler 不会将此项目添加到加载路径,因此您会收到 LoadError。

要修复它,请确保您使用 Bundler 来运行您的应用程序:

$ bundle exec ruby app.rb

或者,您可以在代码中设置 Bundler。添加

require 'bundler/setup'

app.rb每次运行时都会设置 Bundler 。

于 2013-10-22T21:06:24.153 回答