Rails 3.0 Parsing XML and Inserting into Database

后端 未结 3 2008
星月不相逢
星月不相逢 2021-02-11 08:48

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 par

相关标签:
3条回答
  • 2021-02-11 08:52

    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

    0 讨论(0)
  • 2021-02-11 09:00

    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.

    0 讨论(0)
  • 2021-02-11 09:12

    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.

    0 讨论(0)
提交回复
热议问题