nesquena/rabl — Issue #132: Unable to create XML with arbitrary root node
Author: joefiorini
Created: 2011-10-24 14:48:31+00:00
State: open

I'm writing an API and want to offer both XML and JSON. Currently we're using two views (one `.xml.builder` and one `.json.erb` to generate the results). There's a lot of duplication between the two; it's getting quite ugly. Rabl seems like a much more elegant solution to the problem.

Given I have a collection of users (where User is an ActiveRecord model) and I'm using WillPaginate to paginate the result set, how should I recreate the following XML in Rabl?

```xml

1
120
5

Joe
Bob

...

```

I've had a very hard time finding something that works. Trying to add the root node in my Rabl view always gave me a tag `` just inside the `` tag. I extracted the root node into a `users.xml.builder` layout, which helped but I can't get the pagination metadata to show up in the `` tag. Instead I'm getting:

```xml

Joe
Bob
4
32
1

...

```

The code that seemed logical to me (based on my reading of the docs) to generate this was:

```ruby
collection @results
glue @results do
node :current_page do
@results.current_page.to_i
end
attributes :total_pages, :total_entries
end
extends 'users/show'
```

The `users.xml.builder` layout:

```ruby
xml.instruct!
xml.results do
xml << yield
end
```

Any thoughts on the proper way to generate the XML I'm looking for?