Using partials with XML builder, what's the right way?

Hello everyone,

Partials in XML builder are proving to be non-trivial.

After some initial Google searching, I found the following to work,
although it’s not 100%

 xml.foo do
     xml.id(foo.id)
     xml.created_at(foo.created_at)
     xml.last_updated(foo.updated_at)
     foo.bars.each do |bar|
         xml << render(:partial => 'bar/_bar', :locals => { :bar

=> bar })
end
end

this will do the trick, except the XML output is not properly
indented. the output looks something similar to:

<foo>
  <id>1</id>
  <created_at>sometime</created_at>
  <last_updated>sometime</last_updated>
<bar>
  ...
</bar>
<bar>
  ...
</bar>
</foo>

The <bar> element should align underneath the <last_updated>
element, it is a child of <foo> like this:

<foo>
  <id>1</id>
  <created_at>sometime</created_at>
  <last_updated>sometime</last_updated>
  <bar>
    ...
  </bar>
  <bar>
    ...
  </bar>
</foo>

Works great if I copy the content from bar/_bar.xml.builder into the
template, but then things just aren’t DRY.

Thanks,

Matthew

Matthew Hillsborough wrote:

Hello everyone,

Partials in XML builder are proving to be non-trivial.

So use Haml instead of Builder – it works just as well for XML as for
HTML. Problem solved.

IMHO, Builder is the single most pointless thing in the Rails ecosystem.

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

I’ve swapped-in Nokogiri rather than builder, but my general strategy
is the same; using helper methods to encapsulate discreet xml building
code for reuse rather than partials. Seems less hackish. Just pass
in the xml object you are building up.