I'm trying to get a simple image uploader class working in Sinatra with Carrierwave. I've used almost identical code to this and specified the same lines in the Gemfile, but after running $bundle install and everything installing with no problem, I get a LoadError from the following code:
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 config file:
# 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 definition:
class ImageUploader < CarrierWave::Uploader::Base
include CarrierWave::RMagick
process :resize_to_fit => [1024, 1024]
storage :fog
end
My results:
$ 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>'
Can anyone help? Thanks so much.
In your Gemfile
you’ve specified the git version of RMagick. Bundler takes some special steps to add such libraries to Ruby’s load path, but they aren’t available when running without Bundler as they aren’t normal gems. When you run your app without Bunder with ruby app.rb
, Bundler doesn’t get to add this project to the load path, so you get a LoadError.
To fix it, make sure you use Bundler to run your app:
$ bundle exec ruby app.rb
Alternatively you can set up Bundler in your code. Add
require 'bundler/setup'
to the top of app.rb
and Bundler will be setup every time you run.