Here's what people recommend doing to render XML from a HTTP request in Rails:
class WebServiceController < ApplicationController
def people
@people = Person.find :all
respond do |format|
format.xml
end
end
end
And the corresponding
people.xml.builder
in /views/web_service/
:
xml.instruct! :xml, :version=>"1.0"
xml.people do
@people.each do |person|
xml.person do
xml.firstname person.first_name
xml.lastname person.last_name
xml.age person.age
end
end
end
When testing this, I kept getting browser errors about invalid XML. When I looked at the data that was returned from my webservice call, I realised that the XML was generated like I wanted, but it was subsequently embedded into the application layout I am using for all of the views (
views/layouts/application.rhtml
).The issue was resolved by changing my controller code to
class WebServiceController < ApplicationController
def people
@people = Person.find :all
render :template => 'web_service/people.xml.builder', :layout => false
end
end