我正在尝试使用Carrierwave在Sinatra中运行一个简单的图像上传器类。我已经使用了几乎完全相同的代码并在Gemfile中指定了相同的行,但在运行$ bundle install并且所有安装都没有问题之后,我从以下代码中获得了一个LoadError:
的Gemfile:
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>'
有人可以帮忙吗?非常感谢。
答案 0 :(得分:2)
在你的Gemfile
中,你已经指定了RMagick的git版本。 Bundler需要一些特殊的步骤来将这些库添加到Ruby的加载路径中,但是在没有Bundler的情况下运行时它们不可用,因为它们不是普通的gem。当您使用ruby app.rb
运行没有Bunder的应用程序时,Bundler无法将此项目添加到加载路径,因此您会收到LoadError。
要解决此问题,请确保使用Bundler来运行您的应用:
$ bundle exec ruby app.rb
或者,您可以在代码中设置Bundler。添加
require 'bundler/setup'
到app.rb
的顶部,每次运行时都会设置Bundler。