如何解决尝试在导轨下加载宝石时,LoadError没有此类文件
| 当我尝试在Rails控制台中需要一些gem时,出现“没有此类文件”错误。奇怪的是,在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/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\"]
宝石文件:
source \'http://rubygems.org\'
gem \'rails\',\'3.0.5\'
gem \'pg\'
gem \'oauth2\'
gem \'haml\'
解决方法
您不能在Rails中要求Gemfile中没有的gem。您必须将它们包含在Gemfile中,或在需要时指定gem的完整路径,例如
require \"/full/path/to/gem\"
。
这是由于Bundler的工作方式并覆盖了现有的加载路径。如果您在Rails控制台中运行$LOAD_PATH
,您会发现它与IRB中的区别很大。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。