不能在Rails中需要'nokogiri'(但是在irb中工作) [英] Cannot require 'nokogiri' in Rails (but works in irb)

查看:89
本文介绍了不能在Rails中需要'nokogiri'(但是在irb中工作)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我一直遵循教程,并得到了我的Hello World例子 - 也设法将git推送到我的Heroku帐户。我开始按照这里的教程: http://railscasts.com/episodes/190 -screen-scraping-with-nokogiri ,并得到了以下代码在终端工作(在mac上)

  require' rubygems'
需要'nokogiri'
需要'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宝石。我的控制器中的代码是:

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

url =http:// www。 walmart.com/search/search-search.tw?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>

哪一个应该输出标题,就像它在终端窗口中一样。然而,我得到一个错误,说没有这样的文件加载 - nokogiri



任何想法,我要去错误?

如果我做了 gem list 我可以在那里看到nokogiri宝石。






UPDATE



我关闭了本地Rails服务器并重新启动。它正在工作。不知道这是因为工作将gem添加到我的根目录还是进行捆绑安装(如下面的帖子所示)。感谢您的帮助。

解决方案

首先尝试改变你的控制器,使其像

  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

我猜你也在使用bundler,检查你是否包含这个gem并运行bundle install。



你可以在< a href =http://gembundler.com/ =nofollow> http://gembundler.com/

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.

解决方案

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/

这篇关于不能在Rails中需要'nokogiri'(但是在irb中工作)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆