设置Rake任务时遇到问题。这是代码:
task :fetch_games => :environment do
require 'nokogiri'
require 'open-uri'
doc = Nokogiri::XML(open(url))
games = doc.xpath('//game')
games.each do |game|
@data = Game.new(
:name => game.at('name').text,
:publisher => game.at('publisher').text,)
@data.save
if @data.save
puts "Success"
else
puts "Didn't work"
end
end
end
它运行没有错误但在数据库中条目显示:“---!ruby / object:Nokogiri :: XML :: Element {} “
任何帮助都会很棒。谢谢!
答案 0 :(得分:1)
自己想出xpath语法不正确。我需要使用:
/game
而不是
//game