Forum: Ruby on Rails export a table data to xml file using Ruby on Rails

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Devi R. (Guest)
on 2009-01-29 13:41
Dear All,
How to export a table data to xml file using Ruby on Rails. I followed
this code it's created xml file like table.Anyone knows please suggest
me.

Controller
headers['Content-Type'] = "text/xml"
headers['Content-Disposition'] = 'attachment; filename="xml-export.xml"'
headers['Cache-Control'] = ''
@records = Company.find(:all)

View
<table border="1">
  <th>Name</th>
  <th>Address1</th>
  <th>Address2</th>
<% @records.each do |c|  %>

  <tr><td><%= c.name %></td>
  <td><%= c.com_address1 %></td>
  <td><%= c.com_address2 %></td>
  </tr>
<% end %></table>


XML File is
- <table border="1">
  <th>Name</th>
  <th>Address1</th>
  <th>Address2</th>
- <tr>
  <td>xx</td>
  <td>xx</td>
  <td>xx</td>
  </tr>
- </table>
Regards
R.Devi
Saravanan K. (Guest)
on 2009-01-31 13:48
Hi Devi,

Try with following codes. let me know if you find any issues
def xmlfile
  @records = Company.find(:all)
  render :xml=>@records.to_xml
end

def xmlfile
  @records = Company.find(:all)
  render :partial => '/company/xmlfile', :layout => false, :content_type
=> 'application/xml'
end


Thanks
Saravanan K
Devi R. (Guest)
on 2009-02-02 08:28
Thanks for updating and it's working fine.
This topic is locked and can not be replied to.