Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I generate a dynamic XML page using Rails?

I have a function that outputs an XML string:

<expensesAC>
  <cashflow>
    <month>6</month>
    <cash>300</cash>
    <projected>null</projected>
  </cashflow>
  <cashflow>
    <month>6</month>
    <cash>300</cash>
    <projected>null</projected>
  </cashflow>
  <cashflow>
    <month>6</month>
    <cash>300</cash>
    <projected>null</projected>
  </cashflow>
</expensesAC>

I have a view called genxml.xml.erb, and a redirect to mysite.com:3000/genxml.xml.

I need this because flex requires an XML file to be passed. However, what I get is:

<body>
<expensesAC>
  <cashflow>
    <month>6</month>
    <cash>300</cash>
    <projected>null</projected>
  </cashflow>
</expensesAC>
  <cashflow>
    <month>6</month>
    <cash>300</cash>
    <projected>null</projected>
  </cashflow>
  <cashflow>
    <month>6</month>
    <cash>300</cash>
    <projected>null</projected>
  </cashflow>
</body>

So, basically, Rails forces my XML string to be HTML.

How do I generate a dynamic XML page using my string? Or, how do I turn my XML string into an XML page that is dynamically generated? I don't want to create static files, these files need to be generated on the fly because the data in the XML sets will be private information for each user.

like image 929
Sam Avatar asked Jul 02 '09 19:07

Sam


2 Answers

If you want to write XML files with Rails, you might probably want to switch from ERB to Builder template handler.

If you are working with ActiveRecord objects, you can also use the handy :xml option when specifying rendering option.

def index
  @records = Model.all
  respond_to do |format|
    format.html
    format.xml { render :xml => @records }
  end
end
like image 190
Simone Carletti Avatar answered Nov 02 '22 19:11

Simone Carletti


I'm currently doing exactly what you want without a problem. ERB generation of XML is certainly one of the most convenient methods, especially if the output is predominantly the XML template. It also makes it possible to do neat tricks like render partials with collections.

But I know this didn't used to work properly (had to use builders), however I can see that at least from Rails 2.3.5 onwards the ERB approach seems to work fine.

e.g.

class MyController < ApplicationController
  def genxml
    respond_to do |format|
      format.xml 
    end
  end
end

Then genxml.xml.erb produces a valid XML file, and you can even do tricks like:

<expensesAC>
<%= render :partial => 'expenses/cashflow', :collection => @expenses-%>
</expensesAC>

Where expenses/_cashflow.xml.erb is something like:

<cashflow>
  <month><%= cashflow.month %></month>
  <cash><%= cashflow.cash %></cash>
  <projected><%= cashflow.projected %></projected>
</cashflow>

NB: the interleaved <expensesAC> and <cashflow> in your example may indicate there's another problem lurking in your XML generation logic.

like image 28
tardate Avatar answered Nov 02 '22 18:11

tardate