Rails 3.0 Parsing XML and Inserting into Database
I'm new to Rails, and am trying to hook up my application to a third-party API (it does't have a gem or plugin for Rails).
Ideally, what I want to be able to do is parse the data (I've heard good things about Nokogiri, but don't know how to use it for what I want to do do. not for lack of trying), and then insert it into the database.
Could anybody provide instructions or point me in the right direction? Cheers.
UPDATE:
Rake Task:
task :fetch_flyers => :environment do
require 'nokogiri'
require 'open-uri'
doc = Nokogiri::XML(open(url))
events = doc.search('//event')
events.each do |event|
@data = Event.new(
:name => event.at('name').text,
:date => '2011-09-18',
:time => '17:00',
:description => event.at('long_description').text,
:address => event.at('street').text,
:postcode => event.at('postcode').text,
:price => event.at('costs').text,
:user_id => 1,
:genre_id => 1,
:town_id => 1)
@data.save
if @data.save
puts "Success"
else
puts "This didn't save, F***"
end
end
end
I've specified the URL in my code, just hidden it from this code paste. This code does not work. I can not for the life of me figure out why. All I get is the output in terminal saying "This didn't save, F** which means that for some reason the Events are not being saved. Could anyone shed some light on this?
UPDATE 2:
I've checked the URL is correct, and have checked the XML is being parsed correctly by using:
# Printing Out the Variables to make sure they work.
puts @name
puts @date
puts @time
puts @desc
puts @address
puts @postcode
puts @price
puts @user
puts @genre
puts @town
..Which successfully prints out the values in terminal. However, it still won't insert into my database.
My Model is as follows:
belongs_to :user
belongs_to :genre
belongs_to :town
has_attached_file :image, :styles => { :grid => '90x128#', :list => '140x200#', :full => '400x548'}
validates_attachment_content_type :image, :content_type => ['image/jpeg', 'image/png']
before_post_process :normalise_file_name
validates :name, :presence => true
validates :date, :presence => true
validates :time, :presence => true
validates :description, :presence => true
validates :address, :presence => true
validates :town, :presence => true
validates :postcode, :presence => true
validates :price, :presence => true
validates :user_id, :presence => true
validates :viewcount, :presence => true
My Development.log file just shows a load of:
[1m[35mTown Load (0.1ms)[0m SELECT "towns".* FROM "towns" WHERE "towns"."id" = 1 LIMIT 1
[1m[36mTown Load (0.1ms)[0m [1mSELECT "towns".* FROM "towns" WHERE "towns"."id" = 1 LIMIT 1[0m
[1m[35mTown Load (0.1ms)[0m SELECT "towns".* FROM "towns" WHERE "towns"."id" = 1 LIMIT 1
[1m[36mTown 开发者_C百科Load (0.1ms)[0m [1mSELECT "towns".* FROM "towns" WHERE "towns"."id" = 1 LIMIT 1[0m
[1m[35mTown Load (0.1ms)[0m SELECT "towns".* FROM "towns" WHERE "towns"."id" = 1 LIMIT 1
[1m[36mTown Load (0.1ms)[0m [1mSELECT "towns".* FROM "towns" WHERE "towns"."id" = 1 LIMIT 1[0m
[1m[35mTown Load (0.1ms)[0m SELECT "towns".* FROM "towns" WHERE "towns"."id" = 1 LIMIT 1
.......
Every time I try to run the rake task. Does this mean anything?
When using nokogiri you can specify css or xpath selectors, like:
doc = Nokogiri::XML(xml_string)
events = doc.search('//event')
events.each do |event|
puts event.at('short_description')
end
Also, check out nokogiri intro tutorials.
I'd put a puts after getting the URL and another after defining events
to see if there's any data being returned in the first instance. Then I'd check the Event model to see if there are any validations which aren't being met - does the model require a unique user_id or something. I'd also make data a non-instance variable to see if that made a difference. And I'd check that those time formats aren't causing an error in the model.
I just see your log and find out that your Event model do have has_many realtion with Town Model and also consists town_id column in your event table . SO i think that might be the problem as its trying to get the town_id and save the foreign key of town in your event table . But as it doesn't get any so it's always rollback . Also if you want then try to **
@data.save!
instaed of
@data.save
thanks
精彩评论