尝试在rails下加载gem时,LoadError没有这样的文件

时间:2011-04-12 19:28:38

标签: ruby-on-rails-3 rubygems

当我尝试在rails控制台中需要一些宝石时,我收到了“没有这样的文件”错误。奇怪的是,我在irb中要求它时没有出现这个错误。

提前致谢。

        $ irb
irb(main):001:0> $LOAD_PATH
=> ["/usr/local/lib/site_ruby/1.9.1", "/usr/local/lib/site_ruby/1.9.1/x86_64-linux", "/usr/local/lib/site_ruby", "/usr/lib/ruby/vendor_ruby/1.9.1", "/usr/lib/ruby/vendor_ruby/1.9.1/x86_64-linux", "/usr/lib/ruby/vendor_ruby", "/usr/lib/ruby/1.9.1", "/usr/lib/ruby/1.9.1/x86_64-linux"]
        irb(main):002:0> require 'rubygems'
        => true
        irb(main):003:0> require 'image_science'
        => true
        irb(main):004:0> require 'mini_magick'
        => true
        irb(main):005:0> 
        $ rails console
        Loading development environment (Rails 3.0.5)
        irb(main):001:0> require 'rubygems'
        => nil
        irb(main):002:0> require 'image_science'
        LoadError: no such file to load -- image_science
            from /var/lib/gems/1.9.1/gems/activesupport-3.0.5/lib/active_support/dependencies.rb:239:in `require'
    ...
            from script/rails:6:in `require'
            from script/rails:6:in `<main>'
        irb(main):003:0> require 'mini_magick'
        LoadError: no such file to load -- mini_magick
            from /var/lib/gems/1.9.1/gems/activesupport-3.0.5/lib/active_support/dependencies.rb:239:in `require'
    ...
            from script/rails:6:in `require'
            from script/rails:6:in `<main>'
        irb(main):004:0> $LOAD_PATH
        => ["/var/lib/gems/1.9.1/gems/haml-3.0.25/vendor/fssm/lib",  "/home/username/appname/lib", "/home/username/appname/vendor", "/home/username/appname/app/models", "/home/username/appname/app/mailers", "/home/username/appname/app/helpers", "/home/username/appname/app/controllers", "/var/lib/gems/1.9.1/gems/rails-3.0.5/lib", "/var/lib/gems/1.9.1/gems/railties-3.0.5/lib", "/var/lib/gems/1.9.1/gems/thor-0.14.6/lib", "/var/lib/gems/1.9.1/gems/pg-0.10.1/lib", "/var/lib/gems/1.9.1/gems/pg-0.10.1/ext", "/var/lib/gems/1.9.1/gems/oauth2-0.1.1/lib", "/var/lib/gems/1.9.1/gems/multi_json-0.0.5/lib", "/var/lib/gems/1.9.1/gems/haml-3.0.25/lib", "/var/lib/gems/1.9.1/gems/faraday-0.5.7/lib", "/var/lib/gems/1.9.1/gems/multipart-post-1.1.0/lib", "/var/lib/gems/1.9.1/gems/addressable-2.2.4/lib", "/var/lib/gems/1.9.1/gems/activeresource-3.0.5/lib", "/var/lib/gems/1.9.1/gems/activerecord-3.0.5/lib", "/var/lib/gems/1.9.1/gems/arel-2.0.9/lib", "/var/lib/gems/1.9.1/gems/actionmailer-3.0.5/lib", "/var/lib/gems/1.9.1/gems/mail-2.2.15/lib", "/var/lib/gems/1.9.1/gems/treetop-1.4.9/lib", "/var/lib/gems/1.9.1/gems/polyglot-0.3.1/lib", "/var/lib/gems/1.9.1/gems/mime-types-1.16/lib", "/var/lib/gems/1.9.1/gems/actionpack-3.0.5/lib", "/var/lib/gems/1.9.1/gems/tzinfo-0.3.24/lib", "/var/lib/gems/1.9.1/gems/rack-test-0.5.7/lib", "/var/lib/gems/1.9.1/gems/rack-mount-0.6.13/lib", "/var/lib/gems/1.9.1/gems/rack-1.2.1/lib", "/var/lib/gems/1.9.1/gems/erubis-2.6.6/lib", "/var/lib/gems/1.9.1/gems/activemodel-3.0.5/lib", "/var/lib/gems/1.9.1/gems/i18n-0.5.0/lib", "/var/lib/gems/1.9.1/gems/builder-2.1.2/lib", "/var/lib/gems/1.9.1/gems/activesupport-3.0.5/lib", "/var/lib/gems/1.9.1/gems/abstract-1.0.0/lib", "/var/lib/gems/1.9.1/gems/rake-0.8.7/lib", "/var/lib/gems/1.9.1/gems/bundler-1.0.10/lib", "/usr/local/lib/site_ruby/1.9.1", "/usr/local/lib/site_ruby/1.9.1/x86_64-linux", "/usr/local/lib/site_ruby", "/usr/lib/ruby/vendor_ruby/1.9.1", "/usr/lib/ruby/vendor_ruby/1.9.1/x86_64-linux", "/usr/lib/ruby/vendor_ruby", "/usr/lib/ruby/1.9.1", "/usr/lib/ruby/1.9.1/x86_64-linux", "/var/lib/gems/1.9.1/gems/actionpack-3.0.5/lib/action_controller/vendor/html-scanner", "/var/lib/gems/1.9.1/gems/rack-mount-0.6.13/lib/rack/mount/vendor/multimap", "/var/lib/gems/1.9.1/gems/rack-mount-0.6.13/lib/rack/mount/vendor/regin"]

的Gemfile:

source 'http://rubygems.org'

gem 'rails', '3.0.5'

gem 'pg'
gem 'oauth2'
gem 'haml'

1 个答案:

答案 0 :(得分:4)

您不能要求Rails中没有Gemfile中的Gems。您必须将它们包含在Gemfile中,或者在需要时指定gem的完整路径,例如: require "/full/path/to/gem"

这是因为Bundler的工作方式和覆盖现有的加载路径。如果您在Rails控制台中运行$LOAD_PATH,您会发现它与IRB中的显着不同。