Rails:用Nokogiri插入Rake任务解析:---!ruby / object:Nokogiri :: XML :: Element {}到DB

时间:2012-08-17 05:58:07

标签: ruby-on-rails ruby xml rake nokogiri

设置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 {} “

任何帮助都会很棒。谢谢!

1 个答案:

答案 0 :(得分:1)

自己想出xpath语法不正确。我需要使用:

/game

而不是

//game