0

谢谢你的时间!

我的 Windows XP 中安装了 rails。版本是:Rails 3.2.7

我已经安装了 nokogiri 并成功地开发了一个在 Ruby 中使用它的小程序。

但是对于 Rails,我不知道如何在我的代码中包含外部库。

根据这个主题,如何在 Rails 中使用 ruby​​ 库?, 我加gem nokogiri进去了Gemfile。但它仍然给我这个错误:uninitialized constant SayController::Nokogiri

如果我添加require 'nokogiri'我的say_controller.rb,它会给我另一个错误cannot load such file -- nokogiri

我是 Rails 的新手。这似乎是一项非常简单的任务。我该怎么办。

顺便说一句,这是我跑步时得到的bundle install

Using rake (0.9.2.2)
Using i18n (0.6.0)
Using multi_json (1.3.6)
Using activesupport (3.2.7)
Using builder (3.0.0)
Using activemodel (3.2.7)
Using erubis (2.7.0)
Using journey (1.0.4)
Using rack (1.4.1)
Using rack-cache (1.2)
Using rack-test (0.6.1)
Using hike (1.2.1)
Using tilt (1.3.3)
Using sprockets (2.1.3)
Using actionpack (3.2.7)
Using mime-types (1.19)
Using polyglot (0.3.3)
Using treetop (1.4.10)
Using mail (2.4.4)
Using actionmailer (3.2.7)
Using arel (3.0.2)
Using tzinfo (0.3.33)
Using activerecord (3.2.7)
Using activeresource (3.2.7)
Using bundler (1.1.5)
Using coffee-script-source (1.3.3)
Using execjs (1.4.0)
Using coffee-script (2.2.0)
Using rack-ssl (1.3.2)
Using json (1.7.4)
Using rdoc (3.12)
Using thor (0.16.0)
Using railties (3.2.7)
Using coffee-rails (3.2.2)
Using jquery-rails (2.0.2)
Using nokogiri (1.5.5)
Using rails (3.2.7)
Using sass (3.2.0)
Using sass-rails (3.2.5)
Using sqlite3 (1.3.6)
Using uglifier (1.2.7)
Your bundle is complete! Use `bundle show [gemname]` to see where a bundled gem is installed.

这是我的 Gemfile 的样子:

source 'https://rubygems.org'

gem 'rails', '3.2.7'

gem 'sqlite3'
gem 'nokogiri'

group :assets do
  gem 'sass-rails',   '~> 3.2.3'
  gem 'coffee-rails', '~> 3.2.1'

  gem 'uglifier', '>= 1.0.3'
end

gem 'jquery-rails'
4

1 回答 1

1

github nokogiri中所述,我认为您需要在 say_controller.rb 中需要 2 行

例如:

require 'nokogiri'
require 'open-uri'
class ControllerName < ApplicationController
    def index
        doc = Nokogiri::HTML(open('http://www.google.com/search?q=sparklemotion'))
    end
end
于 2012-10-10T08:29:54.753 回答