Export a table data to xml file using Ruby on Rails


#1

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

<% @records.each do |c| %> <% end %>
Name Address1 Address2
<%= c.name %> <%= c.com_address1 %> <%= c.com_address2 %>

XML File is

  • Name Address1 Address2
    xx xx xx

Regards
R.Devi


#2

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


#3

Thanks for updating and it’s working fine.