简体   繁体   中英

How do I get an installed ruby gem included in Rails?

I am attempting to get a gem I installed working in a Rails application. I can require the gem just fine in a Ruby program that I run from the command line using:

require 'nokogiri'

But when I attempt to do the same in one of my Rails controllers it errors saying "no such file to load -- nokogiri".

I tried using the full path to the lib/nokogiri.rb file, but that fails because it cannot find "nokogiri/native".

Better, place the following in your environment.rb file:

Rails::Initializer.run do |config|
  ...
  config.gem :nokogiri
  ...
end

This will tell Rails that you depend on that particular gem. It also allows you to specify particular versions, and it will automatically keep all your gems synched, or unpack them into vendor/gems if you so wish.

I had a similar error but simply forgot to put the following in my environment.rb file: (note the quoted "nokogiri" )

Rails::Initializer.run do |config|
  ...
  config.gem "nokogiri"
  ...
end

Ok I figured it out. This is going to sound pretty stupid...but oh well...

It turns out I had two installations of ruby on my machine. I use InstantRails to serve my test applications and it comes prepackaged with an installation of ruby. I had another installation however outside of this and it was here that nokogiri had been installed, not in the installation in InstantRails.

In any case they were looking in different spots for the gems.

Try the following

require 'rubygems'  
gem 'nokogiri'  

If you are on some form of *nix then did you get any errors when you installed the gem, particularly errors stating that the gem was not on the path. This may happen if you have installed the gem as yourself rather than as root and you do not have your personal gem library in your gem path.

If you always install your gems using

sudo gem install some_gem_name 

then you should not get that problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM