Cannot Require 'Nokogiri' in Rails (But Works in Irb)

Cannot require 'nokogiri' in Rails (but works in irb)

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/

IRB showing LoadError: cannot load such file -- nokogiri error when trying to require nokogiri

All you need to do is to run bundle exec irb to use gems from Gemfile.

How can I use Nokogiri in irb?

Try to require 'rubygems' before requiring nokogiri. If there are no witches on your machine this could help.

Ruby Rails and Webscraping (Nokogiri) and Nitrous.io

Add nokogiri to your gem file, run bundle install and start using it. There is no need for require

After bundle install, you can straight away use it in controller actions. e.g

def index
p = Nokogori::HTML(open('http://domain.com')) #=> p now have the contents of the page
end

Can't run rake task requiring 'nokogiri'

Suggestion: if it's at all feasible for your environment, consider using Bundler. It has a tendency to make a lot of these nightmarish gem issues go away.

require nokogiri: Load Error

Try adding

require 'rubygems'

to the .rb file.

In Ruby 1.9+ rubygem is loaded automatically and not needed. Although there is debate whether that line is required pre 1.9.

Nevertheless, give it a shot.



Related Topics



Leave a reply



Submit