Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Array#to_xml accepts :root_attrs

#1345

You can define root attributes using :root_attrs option:

@projects.to_xml(:root_attrs => {:page => 1, :per_page => 3})

<?xml version="1.0" encoding="UTF-8"?>
<projects page="1" per_page="3" type="array">
  <project>
    <name>project 1</name>
  </project>
  <project>
    <name>project 2</name>
  </project>
  <project>
    <name>project 3</name>
  </project>
</projects>

This is useful for paginated XMLs

Reported by Ernesto Jiménez · November 7th, 2008 @ 06:50 PM

State: wontfix
Milestone: 2.x
Assigned to: nobody
Importance: none

Activity

  1. Luismi Cavallé
    Luismi Cavallé

    +1 Useful patch, tests pass and docs

    November 7th, 2008 @ 10:14 PM

  2. Raul Murciano
  3. Juanjo Bazán
    Juanjo Bazán
    • Tag set to verified

    Nice addition. Works for me and docs look good.

    November 8th, 2008 @ 11:26 AM

  4. Emili Parreño
  5. Deleted User
    Deleted User

    +1, everything ok and is useful

    November 8th, 2008 @ 08:51 PM

  6. DHH
    DHH
    • State changed from new to wontfix

    The problem is that there won't parity with Hash.from_xml, which is the sister method of to_xml. If you give it your sample string, you'll get {"projects"=>[{"name"=>"project 1"}, {"name"=>"project 2"}, {"name"=>"project 3"}]} back -- in other words, the attributes are just dropped.

    Until we find a way of dealing with that, I don't see this as a good fit.

    November 9th, 2008 @ 06:28 PM

  7. Ernesto Jiménez
    Ernesto Jiménez

    It actually seems that the problem is with the type="array".

    http://gist.github.com/23401

    omitting type="array" gets the parameters.

    A possible solution might be avoiding type="array" when :root_attrs is set?

    November 10th, 2008 @ 01:27 AM