I coded a little Ruby script that would parse a remote XML file and extract some data from it using Nokogiri. Now I’m trying to code a more advanced version as a Rails application.
I have my code inside of a controller. It’s similar to the code that I used in my Ruby script, however it’s not working. I believe the error is because it’s trying to load the XML locally rather then externally.
Here is the error that Rails is giving me:
No such file or directory - http://mal-api.com/anime/10?format=xml
Here is a sample of the code in my controller: (I can provide the whole thing if needed, but it’s mainly just the default Rails scaffold code.)
def create
require 'nokogiri'
@anime = Anime.new(params[:anime])
doc = Nokogiri::XML(open("http://mal-api.com/anime/#{@anime.mal_id}?format=xml"))
#Title
title = doc.css("anime english_title").inner_html
#Snipped rails scaffold code
end
mal_id is passed in through a form. Nokogiri is added in my Gemfile.
Is there something I’m missing or that I’ve done wrong?
Any help is appreciated.
By default the
openmethod in ruby is used to open files. If you want to directly open an URL you need torequire 'open-uri'. More information can be found in the docs: http://www.ruby-doc.org/stdlib-1.9.3/libdoc/open-uri/rdoc/OpenURI.html