不能要求“nokogiri”;在 Rails 中(但在 irb 中工作)

发布于 2024-11-09 09:27:42 字数 1538 浏览 0 评论 0原文

我刚刚开始使用 Ruby on Rails,到目前为止它运行良好。我现在正在尝试实现一个 gem,但它不起作用,我希望这只是一个初学者的错误 - 我还没有掌握的东西!

我已经遵循了教程并得到了我的 hello world 示例 - 还设法将其 git Push 到我的 Heroku 帐户。我开始在这里遵循教程: 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

,效果很好。我可以在终端内看到标题。但是,当我尝试将此代码放入视图控制器中时,它找不到 nokogiri gem。我的控制器中的代码是

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

那么在我的 HTML 中,我

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

应该输出标题,就像在终端窗口中一样。但是,我收到一条错误消息,提示 no such file to load -- nokogiri

有什么想法我哪里出错了吗?
如果我执行gem list,我可以在那里看到 nokogiri gem。


更新

我关闭了本地rails服务器并重新启动。现在已经开始工作了。不确定是否是因为将 gem 添加到我的根目录或进行“捆绑安装”的工作(如下面的帖子建议的那样)。感谢您的帮助。

I've just started using Ruby on Rails and so far it's working nicely. 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. 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)

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

<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

Any ideas where I'm going wrong?
If I do gem list I can see the nokogiri gem there.


UPDATE

I closed the local rails server and restarted. 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). Thanks for the help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一绘本一梦想 2024-11-16 09:27:42

首先尝试将您的控制器更改为类似

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

我猜您也在使用捆绑器的东西,检查您是否已包含此 gem 并运行捆绑安装。

您可以在 http://gembundler.com/ 找到相关文档

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.

You can find the documentation for this at http://gembundler.com/

眼角的笑意。 2024-11-16 09:27:42
  1. 您应该将各种 require 语句放在 index 方法之外。将它们放在那里并不会减慢您的速度,但是您要求 Ruby 确保每次调用该方法时都加载它们。最好只在代码顶部需要它们一次。但是,这不会导致您的问题。

  2. 我怀疑您在与 IRB 运行的环境不同的环境下测试服务器。请尝试暂时删除有问题的 require 语句,并将 HTML 模板更改为:

    环境

    • Ruby:<%=RUBY_VERSION%>
    • 宝石:<%=`宝石列表`%>
  1. You should put your various require statements outside of your index method. 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. 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:

    <h1>Environment</h1>
    <ul>
      <li>Ruby: <%=RUBY_VERSION%></li>
      <li>Gems: <%=`gem list`%></li>
    </ul>
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文