title: Ruby on Rails: Using XML Builder Partials
body: <p>Partials in XML builder are proving to be non-trivial.</p>
  
  <p>After some initial Google searching, I found the following to work, although it's not 100%</p>
  
  <pre><code> 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 &lt;&lt; render(:partial =&gt; 'bar/_bar', :locals =&gt; { :bar =&gt; bar })
       end
   end
  </code></pre>
  
  <p>this will do the trick, except the XML output is not properly indented. the output looks something similar to:</p>
  
  <pre><code>&lt;foo&gt;
    &lt;id&gt;1&lt;/id&gt;
    &lt;created_at&gt;sometime&lt;/created_at&gt;
    &lt;last_updated&gt;sometime&lt;/last_updated&gt;
  &lt;bar&gt;
    ...
  &lt;/bar&gt;
  &lt;bar&gt;
    ...
  &lt;/bar&gt;
  &lt;/foo&gt;
  </code></pre>
  
  <p>The <code>&lt;bar&gt;</code> element should align underneath the <code>&lt;last_updated&gt;</code> element, it is a child of <code>&lt;foo&gt;</code> like this:</p>
  
  <pre><code>&lt;foo&gt;
    &lt;id&gt;1&lt;/id&gt;
    &lt;created_at&gt;sometime&lt;/created_at&gt;
    &lt;last_updated&gt;sometime&lt;/last_updated&gt;
    &lt;bar&gt;
      ...
    &lt;/bar&gt;
    &lt;bar&gt;
      ...
    &lt;/bar&gt;
  &lt;/foo&gt;
  </code></pre>
  
  <p>Works great if I copy the content from bar/_bar.xml.builder into the template, but then things just aren't DRY.</p>