Using local gems

Hello,

I have successfully installed some local gems on my rails host. I checked that through irb:

irb(main):002:0> require 'rubygems' => true irb(main):003:0> require_gem 'mylocalgem' => true irb(main):003:0> a = MyLocalGem::MyClass.new => #<MyLocalGem::MyClass:0x409a343c [bunch of stuff from MyClass]>

I'm still unable to make my rails application use that gem.

config/environment.rb (at the end): ENV['GEM_PATH'] = '/usr/lib/ruby/gems/1.8:/my/path/to/local/gems'

app/helpers/application_helper.rb: require 'rubygems' require_gem 'mylocalgem' module ApplicationHelper end

app/controllers/user_controller.rb: [...] def show_myclass    @myclass = MyLocalGem::MyClass.new end [...]

And when I browse to /user/show_myclass, I get: NameError in UserController#show_myclass uninitialized constant UserController::MyLocalGem #{RAILS_ROOT}/app/controllers/user_controller.rb:265:in `show_myclass'

What did I miss?

Thanks, Karine

Hi Karine

How about installing Aptana Radrails, there is a GEM manager for you to use.

ApplicationHelper is really not for that. it's probably not even loaded at the point your show_myclass action is called. Stick your require_gem 'mylocalgem' in environment.rb

Fred

I only have SSH access to that host, sorry.

Karine

OK, thanks for that good practice information, though it did not fix the problem.

I should note the same rails app is working fine on my development server, where the gem is installed in /usr/lib... (and I only need "require 'mylocalgem'" to make it work, of course). So it's really a configuration problem involving gems installed in a local prefix.

Karine

app/helpers/application_helper.rb: require 'rubygems' require_gem 'mylocalgem' module ApplicationHelper end

ApplicationHelper is really not for that. it's probably not even loaded at the point your show_myclass action is called. Stick your require_gem 'mylocalgem' in environment.rb

OK, thanks for that good practice information, though it did not fix the problem.

You might need the ENV['GEM_PATH'] stuff before rubygems is required.
have you tried doing export GEM_PATH=foo in a shell before starting
the mongrels (or whatever it is you're using) ?

Is it still throwing an uninitalized constant error or is the require
itself failing ? If the require isn't failing then it's probably
nothing to do with where the gem is etc...

Fred

You might need the ENV['GEM_PATH'] stuff before rubygems is required.

Wow, indeed, that was so obvious I'm ashamed I didn't think about it. The problem is still there though.

have you tried doing export GEM_PATH=foo in a shell before starting the mongrels (or whatever it is you're using) ?

I'm not starting anything, my host has its webserver configured properly to serve rails apps. GEM_PATH is in my .bash_profile, which may have helped irb to find the gem, but I'm surprised ENV['GEM_PATH'] in environment.rb is not enough to make it work for the rails app.

Is it still throwing an uninitalized constant error or is the require itself failing ? If the require isn't failing then it's probably nothing to do with where the gem is etc...

Yes, still the same unitialized constant error.

Thanks for trying to nail down the issue, I guess I will have to ask my host if they have any specific config that causes this problem...

Karine

Surely there is something you can start or restart: if you don't restart whatever is picking up your rails code then it won't pick up changes to environment.rb It's entirely possible .bash_profile only read when a shell is invoked, and not in other cases (I can never remember the difference between all those files)

Fred

Well done! I killed the running dispatch.fcgi processes, a new one was spawned and the config files re-read. Everything is working fine now, many thanks :slight_smile:

Karine