views:

29

answers:

1

I've been playing around with rail's to_xml, trying to create a really simple rest interface for a project i'm working on.

So far, i've come up with

cards = Card.all(:conditions => {:racedate => Date.today.to_s})
render :xml => cards.to_xml(:include => {:races => { :only => [:id, :number, :race_time, :name] } }, :skip_types => true, :root => "cards")    

in my controller.

This produces some xml.

    <card>
     <country-code>USA</country-code>
     <id>55</id>
     <name>Vernon Downs</name>
     <races>
      <race>
        <id>355</id>
        <name/>
        <number>1</number>
        <race-time/>
      </race>
    </races>
   </card>

What i'd really like is to use xml attributes rather than child nodes, so it would be

<card country-code="USA" id=55 name="Vernon Downs"/> etc.

I've poured over the to_xml api docs but can't seem to find any way of doing this? Do i need to create an xml template and render that way?

Thanks

A: 

I couldn't figure out a nicer way to do this, so i ended up with a view to render the xml as i wanted.

Code for anyone that's interested..

Controller:

    @cards = Card.all(:select => "id,name,country_code,racedate,track_code", :conditions => {:racedate => Date.today.to_s})

   response.headers['Content-type'] = 'text/xml; charset=utf-8'
   render :layout => false

View :

    <?xml version="1.0" encoding="UTF-8"?>
<cards>
<% @cards.each do |card| -%>
    <card card-name="<%= card.name %>" id="<%= card.id %>" country-code="<%= card.country_code %>" card-date="<%= card.racedate %>" track-code="<%= card.track_code %>">
        <races>
    <% card.races.each do |race| -%>
        <race name="<%= race.name %>" id="<%= race.id %>" tote-race-number="<%= race.number %>" post-time="<%= race.race_time %>"></race>
    <% end -%>
    </races>
    </card>
<% end -%>
</cards>
George