How do I generate a dynamic XML page using Rails?

apache-flex, ruby, ruby-on-rails, xml

Solution

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

Problem

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.

Original source