onrails.org home

Xml Builder

I created the new Playground category on this blog to expose various aspects regarding Ruby On Rails that I am exploring. It may raise more questions than provide answers, but in any case don’t hesitate to jump in and add your 2 cents.

The xml_builder method here after uses the render_to_string method to create some xml structure. The xmlstring could as well have been in a separate .rxml file and and a simple render statement instead of render_as_string could have saved one line of code. But hey, that’s what the playground is for!

class PlaygroundController < ApplicationController

def xml_builder xml_string = <<-XML_END xml.test do xml.language(name) xml.description(“Rocks!”) end XML_END result = render_to_string(:inline => xml_string, :locals => { :name => ’Ruby’}, :type => :rxml) render_text result end

end

The output:


Ruby
Rocks!

Fork me on GitHub