Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom Gem in /vendor/gems not Loading

I recently decided to take some functionality that I have in several of my Rails apps and extract it into an Engine. I have now finished the Engine and I'm trying to install the finished gem into one of my apps.

This particular gem isn't something that I want to be public, so I packaged the gem with gem build my_gem.gemspec and then put the packaged gem in the vendor/gems folder of my application. Then, I added gem 'my_gem', '0.0.1', :path => 'vendor/gems' to my gemfile and ran bundle install.

Unfortunately though, Rails doesn't seem to be loading the gem and I can't seem to require it manually:

$ 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>'

Am I doing something wrong? How do I fix this?


Edit: Here's my gem environment information.

$ 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/
like image 385
Ajedi32 Avatar asked Oct 05 '22 06:10

Ajedi32


1 Answers

Type gem env to check where Ruby is searching for gems.

You then want to append the directory you put your gem in, to the GEM_PATH environment variable. e.g.:

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

See also: http://docs.rubygems.org/read/chapter/12

If you are using a Gemfile, you can also do this:

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

(you have to add the name of your gem directory to the path)

like image 197
Tilo Avatar answered Oct 13 '22 12:10

Tilo