NameError:未初始化的常量:: Nokogiri

Ива*_*вац 4 ruby rspec ruby-on-rails nokogiri

我为模型写了测试:

describe Video do
  describe 'searching youtube for video existence' do
    it 'should return true if video exists' do
      Video.video_exists?("http://www.youtube.com/watch?v=KgfdlZuVz7I").should be_true
    end
  end  
end
Run Code Online (Sandbox Code Playgroud)

这是型号代码:

class Video < ActiveRecord::Base
  attr_accessible :video_id

  def self.video_exists?(video_url)
    video_url =~ /\?v=(.*?)&/
    xmlfeed = Nokogiri::HTML(open("http://gdata.youtube.com/feeds/api/videos?q=#{$1}"))
    if xmlfeed.at_xpath("//openSearch:totalResults").content.to_i == 0
      return false
    else
      return true
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

但它失败了,错误:

Failures:

  1) Video searching youtube for video existence should return true if video exists
     Failure/Error: Video.video_exists?("http://www.youtube.com/watch?v=KgfdlZuVz7I").should be_true
     NameError:
       uninitialized constant Video::Nokogiri
     # ./app/models/video.rb:6:in `video_exists?'
     # ./spec/models/video_spec.rb:6:in `block (3 levels) in <top (required)>'

Finished in 0.00386 seconds
1 example, 1 failure
Run Code Online (Sandbox Code Playgroud)

我不知道如何解决这个问题,可能会出现什么问题?

Ива*_*вац 10

问题是因为我没有添加gem nokogiri到Gemfile.

加入之后我删除require 'nokogiri',并require 'open-uri'从模型和它的作品.