views:

456

answers:

3
+1  Q: 

Rails and XML

Hi So I have a function that outputs an xml string that goes something like

<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>

Now, 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 end up getting 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? Like, how do I turn my xml string into an xml page, 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.

+1  A: 

In your controller, you'll want to render the content as xml. Something like:

def show
  respond_to do |format|
    format.html
    format.xml { render :xml => my_function_that_outputs_xml }
  end
end
Aaron Hinni
+4  A: 

If you want to write XML files with Rails, you might probably want to swith 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
Simone Carletti
As @weppos points out make sure you rename `genxml.xml.erb` to `genxml.xml.builder` or `genxml.xml.rxml`
Scott
weppos, you beat me to it, that's the anwser I would have given
marcgg
There's nothing wrong with generating XML using ERB (or HAML, or any other templating engine you prefer), though.
Greg Campbell
A: 

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.

tardate