데이터베이스를 가져 오려고하는 xml 데이터가 있습니다. nokogiri gem을 사용하려고합니다. xml_data.rb라는 파일을 작성했습니다.레일에서 XML 데이터를 가져 오는 방법 5
xml_data.rb:8:in
block in <main>': uninitialized constant Job (NameError) from C:/RailsInstaller/Ruby2.3.0/lib/ruby/gems/2.3.0/gems/nokogiri-1.7.0.1-x86-mingw32/lib/nokogiri/xml/node_set.rb:187:in
block in each' from C:/RailsInstaller/Ruby2.3.0/lib/ruby/gems/2.3.0/gems/nokogiri-1.7.0.1-x86-mingw32/lib/nokogiri/xml/node_set.rb:186:inupto' from C:/RailsInstaller/Ruby2.3.0/lib/ruby/gems/2.3.0/gems/nokogiri-1.7.0.1-x86-mingw32/lib/nokogiri/xml/node_set.rb:186:in
each' from xml_data.rb:7:in `'
내가 제대로이 작업을 수행하는 방법을 알고 있어야하지 않아요 : 나는 명령 줄에서 >ruby xml_data.rb
를 실행하면, 나는 다음과 같은 오류를받을.
require 'nokogiri'
OLD_DATA = 'data/old_data.xml'
doc = File.open(OLD_DATA) {|f| Nokogiri::XML(f)}
doc.css('request').each do |node|
Job.create(
:last_name => node['name'],
:telephone => node['phone'],
:street_address => node['address'],
:city => node['city'],
:state => node['state'],
:zip => node['zip'],
:email => node['email'],
:au_chog => node['chogAu'],
:person_type => node['affil'],
:research_use => node['use'],
:subject => node['subject'],
:notes => node['note'],
:start_date => node['startDate'],
:end_date => node['addDate'],
:complete => true,
:time_spend => node['hours']
)
end
루비 : v2.3.3 레일 : V5.0.1
UPDATE
나는 레이크 작업이 될이 코드를 재 작성 아래 xml_data.rb 파일입니다. 그러나, 나는 이것을하지 않았다. 작업을 실행할 때 XML 구문과 관련이있는 몇 가지 구문 오류가 발생합니다. 업데이트 된 코드는 다음과 같습니다.
require 'nokogiri'
namespace :db do
namespace :seed do
desc 'Add old data to database'
task :import_data => :environment do
OLD_DATA = "#{Rails.root}/lib/assets/data/old_data.xml"
doc = Nokogiri::XML(File.open(OLD_DATA)) do |config|
config.options = Nokogiri::XML::ParseOptions::STRICT
end
doc.css("request").each do |node|
Job.create(
:last_name => node['name'],
:telephone => node['phone'],
:street_address => node['address'],
:city => node['city'],
:state => node['state'],
:zip => node['zip'],
:email => node['email'],
:au_chog => node["chogAu"],
:person_type => node['affil'],
:research_use => node['use'],
:subject => node['subject'],
:notes => node['note'],
:start_date => node['startDate'],
:end_date => node['addDate'],
:complete => true,
:time_spend => node['hours']
)
end
end
end
end
구문 오류가 계속 발생합니까? 이것은 실행해야하는 것 같습니다 ... – Glyoko
나는 아니에요. 나는 문제를 일으키는 선이 있었다. 내가 그것을 발견했을 때, 나는 그 글을 업데이트했다. – pshowell