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
What Is the Fully Qualified Name of a Model in Ruby on Rails
Setting Up a Polymorphic Association
How to Add "Access-Control-Allow-Origin" Headers to API Response in Ruby
Delete from Database with Specific Details in Ruby
How to Run Selenium Script on Ie
Jruby on Rails VS. Ruby on Rails, What's Difference
Testing for Empty or Nil-Value String
Bind and Destructure Block Arguments
How to Translate Arabic/Persian Numbers to English Using Ruby
How to Require Activerecord in Irb
Ruby on Rails Group_By (How to Group Events by Month)