1

我正在尝试创建一个循环以在我的数据库中为每个 content_option 创建一条记录。我有它,所以它会创建一个选项,但不会创建另一个选项,我不知道为什么。我尝试复制我在谷歌上找到的其他代码,但它只做了 1 个条目。

    <?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>event@event.com</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
4

1 回答 1

1

更改这行代码:

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

对此:

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

当然它只会显示一个条目;在您的 XML 中,只有一个content_options元素,并且有 2 个content_option元素。

于 2013-01-14T16:57:50.953 回答