自定义Gem in / vendor / gems not Loading

时间:2013-04-28 21:18:16

标签: ruby-on-rails ruby-on-rails-3 gem rails-engines

我最近决定在我的几个Rails应用程序中使用一些功能并将其解压缩到引擎中。我现在已经完成了引擎,我正在尝试将完成的gem安装到我的一个应用程序中。

这个特殊的gem不是我想要公开的东西,所以我用gem build my_gem.gemspec打包了gem,然后将打包的gem放在我的应用程序的vendor / gems文件夹中。然后,我将gem 'my_gem', '0.0.1', :path => 'vendor/gems'添加到我的gemfile并运行bundle install

不幸的是,Rails似乎没有加载宝石,我似乎无法手动要求它:

$ bundle exec rails console --sandbox
Loading development environment in sandbox (Rails 3.2.11)
Any modifications you make will be rolled back on exit
irb(main):001:0> MyGem
NameError: uninitialized constant MyGem
        from (irb):1
        from c:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-3.2.11/lib/rails/commands/console.rb:47:in `start'
        from c:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-3.2.11/lib/rails/commands/console.rb:8:in `start'
        from c:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-3.2.11/lib/rails/commands.rb:41:in `<top (required)>'
        from script/rails:6:in `require'
        from script/rails:6:in `<main>'
irb(main):002:0> require 'my_gem'
LoadError: cannot load such file -- my_gem
        from c:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activesupport-3.2.11/lib/active_support/dependencies.rb:251:in `require'
        from c:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activesupport-3.2.11/lib/active_support/dependencies.rb:251:in `block in require'
        from c:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activesupport-3.2.11/lib/active_support/dependencies.rb:236:in `load_dependency'
        from c:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activesupport-3.2.11/lib/active_support/dependencies.rb:251:in `require'
        from (irb):2
        from c:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-3.2.11/lib/rails/commands/console.rb:47:in `start'
        from c:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-3.2.11/lib/rails/commands/console.rb:8:in `start'
        from c:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-3.2.11/lib/rails/commands.rb:41:in `<top (required)>'
        from script/rails:6:in `require'
        from script/rails:6:in `<main>'

我做错了吗?我该如何解决这个问题?


编辑:这是我的宝石环境信息。

$ gem env
RubyGems Environment:
  - RUBYGEMS VERSION: 1.8.16
  - RUBY VERSION: 1.9.3 (2012-02-16 patchlevel 125) [i386-mingw32]
  - INSTALLATION DIRECTORY: c:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1
  - RUBY EXECUTABLE: c:/RailsInstaller/Ruby1.9.3/bin/ruby.exe
  - EXECUTABLE DIRECTORY: c:/RailsInstaller/Ruby1.9.3/bin
  - RUBYGEMS PLATFORMS:
    - ruby
    - x86-mingw32
  - GEM PATHS:
     - c:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1
     - c:/Users/Ajedi32/.gem/ruby/1.9.1
  - GEM CONFIGURATION:
     - :update_sources => true
     - :verbose => true
     - :benchmark => false
     - :backtrace => false
     - :bulk_threshold => 1000
  - REMOTE SOURCES:
     - http://rubygems.org/

1 个答案:

答案 0 :(得分:4)

键入gem env以检查Ruby搜索gem的位置。

然后,您希望将放置gem的目录附加到GEM_PATH环境变量中。 e.g:

  export GEM_PATH="./vendor/gems:$GEM_PATH"

另请参阅:http://docs.rubygems.org/read/chapter/12

如果您使用的是Gemfile,也可以这样做:

  gem 'my-gem', '0.0.1', :path => 'vendor/gems/my-gem'

(您必须将gem目录的名称添加到路径中)