2013-01-14 95 views
1

我想創建一個循環來創建一個記錄在我的數據庫中的每個content_option。我有它,所以它會創建一個選項,但不是另一個,我不知道爲什麼。我試圖複製我在谷歌上找到的其他代碼,但它只有1個條目。我不能讓Nokogiri循環通過兒童節點

<?xml version="1.0" encoding="UTF-8" ?> 
     <event> 
      <name>My Event</name> 
      <legacy_id>54321</legacy_id> 
      <domain_id>1</domain_id> 
      <description>This is a Auction</description> 
      <event_type></event_type> 
      <starts_at></starts_at> 
      <street_1></street_1> 
      <city></city> 
      <state></state> 
      <zip></zip> 
      <country></country> 
      <phone_1></phone_1> 
      <fax></fax> 
      <content_options> 
       <content_option> 
        <contentable_type>Event</contentable_type> 
        <contentable_id>54321</contentable_id> 
        <key>terms_and_conditions</key> 
        <content>Some Terms and Conditions</content> 
       </content_option> 
       <content_option> 
        <contentable_type>Event</contentable_type> 
        <contentable_id>54321</contentable_id> 
        <key>email</key> 
        <content>[email protected]</content> 
       </content_option> 
      </content_options> 
      <event_links> 
       <link> 
        <name>Brochure</name> 
        <event_id>54321</event_id> 
        <uri>http://www.mysite.com</uri> 
       </link> 
       <link> 
        <name>Auction Documents</name> 
        <event_id>54321</event_id> 
        <uri>http://www.auctiondocuments.com</uri> 
       </link> 
      </event_links> 
      <phone_numbers> 
       <phone_number> 
        <number_type>Approvals</number_type> 
        <event_id>54321</event_id> 
        <name></name> 
        <number></number> 
       </phone_number> 
       <phone_number> 
        <number_type></number_type> 
        <event_id></event_id> 
        <name></name> 
        <number></number> 
       </phone_number> 
      </phone_numbers> 
      <lots> 
       <lot> 
        <legacy_id>12345</legacy_id> 
        <number>1</number> 
        <title>Big Cow</title> 
        <description>A big cow</description> 
        <position>1</position> 
        <price>500</price> 
        <updates> 
         <update> 
          <lot_id>12345</lot_id> 
          <details>Color is blue</details> 
         </update> 
        </updates> 
        <images> 
         <image>http://the.image.is/here.jpg</image> 
        </images> 
        <catalog_images> 
         <catalog_image>http://the.image.is/here.jpg</catalog_image> 
        </catalog_images> 
        <videos> 
         <video>http://the.video.is/here.flv</video> 
        </videos> 
       </lot> 
      </lots> 
     </event> 

軌控制器

def upload_xml 
     xml = params[:file].read 
     xmldoc = Nokogiri::XML(xml) 

     @event = Event.find_by_legacy_id(xmldoc.xpath("event/legacy_id").text) 
     if @event.nil? 
      @event = Event.new 
      @event.legacy_id = xmldoc.xpath("event/legacy_id").text 
      @event.name = xmldoc.xpath("event/name").text 
      @event.domain_id = xmldoc.xpath("event/domain_id").text 
      @event.description = xmldoc.xpath("event/description").text 
      @event.event_type = xmldoc.xpath("event/event_type").text 
      @event.starts_at = xmldoc.xpath("event/starts_at").text 
      @event.street_1 = xmldoc.xpath("event/street_1").text 
      @event.city = xmldoc.xpath("event/city").text 
      @event.state = xmldoc.xpath("event/state").text 
      @event.zip = xmldoc.xpath("event/zip").text 
      @event.country = xmldoc.xpath("event/country").text 
      @event.phone_1 = xmldoc.xpath("event/phone_1").text 
      @event.fax = xmldoc.xpath("event/fax").text 
      @event.save 

      content_options = xmldoc.xpath("//content_options") 

      content_options.each do |option| 
       @content_option = ContentOption.new 
       @content_option.contentable_type = option.at("contentable_type").text 
       @content_option.contentable_id = option.at("contentable_id").text 
       @content_option.key = option.at("key").text 
       @content_option.content = option.at("content").text 
       @content_option.save 
      end 

     else 



     end 
end 
+0

'find_or_create_by _...'構造會簡化你的代碼。 –

回答

2

更改這行代碼:

content_options = xmldoc.xpath("//content_options") 

這樣:

content_options = xmldoc.xpath("//content_option") 

當然它只會告訴你一個條目;在您的XML中,只有一個content_options元素,並且有2個​​元素。