简体   繁体   English

在Rails中不能要求'nokogiri'(但在irb中工作)

[英]Cannot require 'nokogiri' in Rails (but works in irb)

I've just started using Ruby on Rails and so far it's working nicely. 我刚刚开始使用Ruby on Rails,到目前为止它运行良好。 I'm now trying to implement a gem but it's not working, and I am hoping it's just a beginner mistake - something which I've not yet grasped!! 我现在正在尝试实现一个宝石,但它不起作用,我希望它只是一个初学者的错误 - 我尚未掌握的东西!

I've followed tutorials and got my hello world example - also managed to git push this to my Heroku account. 我已经按照教程并获得了我的hello world示例 - 也设法将git推送到我的Heroku帐户。 I started to follow the tutorial here: http://railscasts.com/episodes/190-screen-scraping-with-nokogiri and got the following code working in Terminal (on mac) 我开始按照这里的教程: http//railscasts.com/episodes/190-screen-scraping-with-nokogiri并在终端中使用以下代码(在mac上)

require 'rubygems'
require 'nokogiri'
require 'open-uri'

url = "http://www.walmart.com/search/search-ng.do?search_constraint=0&ic=48_0&search_query=batman&Find.x=0&Find.y=0&Find=Find"
doc = Nokogiri::HTML(open(url))
puts doc.at_css("title").text

So that worked nicely. 所以这很好用。 I can see the title inside of terminal. 我可以在终端内看到标题。 However, when I try to put this code in my view controller, it cannot find the nokogiri gem. 但是,当我尝试将此代码放在我的视图控制器中时,它找不到nokogiri gem。 The code in my controller is 我的控制器中的代码是

class HomeController < ApplicationController
    def index
        require 'rubygems'
        require 'nokogiri'
        require 'open-uri'

        url = "http://www.walmart.com/search/search-ng.do?search_constraint=0&ic=48_0&search_query=batman&Find.x=0&Find.y=0&Find=Find"
        doc = Nokogiri::HTML(open(url))
        @mattVar = doc.at_css("title").text
    end
end

So then in my HTML, I have 那么在我的HTML中,我有

<h1>Hello!</h1>
<p><%= @mattVar %></p>

Which should output the title as it does in the terminal window. 哪个应该在终端窗口中输出标题。 However, I get an error saying no such file to load -- nokogiri 但是,我得到一个错误,说no such file to load -- nokogiri

Any ideas where I'm going wrong? 我出错的任何想法?
If I do gem list I can see the nokogiri gem there. 如果我做gem list我可以看到那里的nokogiri宝石。


UPDATE UPDATE

I closed the local rails server and restarted. 我关闭了本地rails服务器并重新启动。 It's now working. 它现在正在运作。 Not sure if it was because of the work adding the gem to my root or doing the 'bundle install' (as the posts below suggested). 不确定是不是因为工作将gem添加到我的root或执行'bundle install'(如下面建议的帖子)。 Thanks for the help. 谢谢您的帮助。

Firstly try changing your controller to be somthing like 首先尝试将控制器更改为类似的东西

class HomeController < ApplicationController
    require 'nokogiri'
    require 'open-uri'

    def index
        url = "http://www.walmart.com/search/search-ng.do?search_constraint=0&ic=48_0&search_query=batman&Find.x=0&Find.y=0&Find=Find"
        doc = Nokogiri::HTML(open(url))
        @mattVar = doc.at_css("title").text
    end
end

I'm guessing you are also using bundler, check that you have include this gem and run bundle install. 我猜你也在使用bundler,检查你是否包含了这个gem并运行bundle install。

You can find the documentation for this at http://gembundler.com/ 您可以在http://gembundler.com/找到相关文档。

  1. You should put your various require statements outside of your index method. 您应该将各种require语句放在index方法之外。 It won't slow you down much to have them in there, but you're asking Ruby to ensure that they are loaded every time the method is called. 将它们放在那里不会减慢你的速度,但是你要求Ruby确保每次调用方法时都加载它们。 Better to just require them once at the top of your code. 最好只需要在代码顶部一次。 This won't cause your problem, however. 但是,这不会导致您的问题。

  2. I suspect that you are testing your server under a different environment than what IRB is running in. Try removing the offending require statements for the moment and changing your HTML template to: 我怀疑您在不同于IRB运行的环境下测试服务器。暂时尝试删除有问题的require语句并将HTML模板更改为:

     <h1>Environment</h1> <ul> <li>Ruby: <%=RUBY_VERSION%></li> <li>Gems: <%=`gem list`%></li> </ul> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Rails不能要求&#39;nokogiri / nokogiri&#39;,尽管IRB可以 - Rails Cannot require 'nokogiri/nokogiri', though IRB can rails console需要nokogiri返回false(但在irb中工作) - rails console require nokogiri return false (but works in irb) IRB在尝试要求“nokogiri”时显示“LoadError:无法加载此类文件 - nokogiri”错误 - IRB showing “LoadError: cannot load such file — nokogiri” error when trying to require “nokogiri” &#39;require&#39;: 无法加载此类文件 -- 运行 `rails server` 时出现 &#39;nokogiri\\nokogiri&#39; (LoadError) - 'require': cannot load such file -- 'nokogiri\nokogiri' (LoadError) when running `rails server` Gem 在 IRB 中有效,但在 Rails 控制台中无效 - Gem works in IRB but not Rails console 如何修复“require”:无法加载此类文件 — nokogiri/2.1/nokogiri (LoadError) - How to fix `require': cannot load such file — nokogiri/2.1/nokogiri (LoadError) 无法在Rails中加载RMagic,但可以在IRB中加载 - Cannot Load RMagic in Rails, but can in IRB 无法加载此类文件 - Nokogiri rails - cannot load such file - Nokogiri rails 调用irb和rails console中的'rubygems'结果=> false。 - Calling require 'rubygems' in irb and rails console results => false. Rails的创建方法不向表中添加记录,但适用于IRB - Rails create method not adding record to the table, but works for the IRB
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM