如何将已安装的ruby gem包含在Rails中?

时间:2021-12-15 00:12:47

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:

我正在尝试在Rails应用程序中安装gem。我可以在使用以下命令行运行的Ruby程序中使用gem:

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".

但是当我试图在我的一个Rails控制器中做同样的事情时,它会说“没有这样的文件来加载——nokogiri”。

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

我尝试使用lib/nokogiri的完整路径。rb文件,但是失败了,因为它找不到“nokogiri/native”。

4 个解决方案

#1


3  

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

最好在您的环境中放置以下内容。rb文件:

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.

这将告诉Rails您依赖于那个特定的gem。它还允许您指定特定的版本,并且它将自动保持所有的gem同步,或者如果您愿意,可以将它们解压缩到供应商/gems中。

#2


2  

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

我也有过类似的错误,但只是忘记在我的环境中添加以下内容。rb文件:(注意引用的“nokogiri”)

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

#3


1  

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.

我的机器上有两个ruby安装。我使用InstantRails来服务我的测试应用程序,它与ruby的安装一起预先打包。我有另一个装置,但是在这个之外,nokogiri在这里被安装,而不是在InstantRails的安装中。

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

无论如何,他们在不同的地点寻找宝石。

#4


0  

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.

如果您在某种形式的*nix上,那么您在安装gem时就会出现错误,尤其是错误声明gem不在路径上。这可能发生在你作为你自己安装了宝石而不是作为根而你在你的宝石路径中没有你的个人宝石库。

If you always install your gems using

如果你总是安装宝石使用

sudo gem install some_gem_name 

then you should not get that problem.

那你就不应该有这个问题。

#1


3  

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

最好在您的环境中放置以下内容。rb文件:

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.

这将告诉Rails您依赖于那个特定的gem。它还允许您指定特定的版本,并且它将自动保持所有的gem同步,或者如果您愿意,可以将它们解压缩到供应商/gems中。

#2


2  

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

我也有过类似的错误,但只是忘记在我的环境中添加以下内容。rb文件:(注意引用的“nokogiri”)

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

#3


1  

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.

我的机器上有两个ruby安装。我使用InstantRails来服务我的测试应用程序,它与ruby的安装一起预先打包。我有另一个装置,但是在这个之外,nokogiri在这里被安装,而不是在InstantRails的安装中。

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

无论如何,他们在不同的地点寻找宝石。

#4


0  

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.

如果您在某种形式的*nix上,那么您在安装gem时就会出现错误,尤其是错误声明gem不在路径上。这可能发生在你作为你自己安装了宝石而不是作为根而你在你的宝石路径中没有你的个人宝石库。

If you always install your gems using

如果你总是安装宝石使用

sudo gem install some_gem_name 

then you should not get that problem.

那你就不应该有这个问题。