Forum: Ruby on Rails Define id in xml

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Fde34ab22ac6ae44693f36b9f7fed41d?d=identicon&s=25 Brijesh Shah (brijesh)
on 2009-01-06 11:53
Hi

I like to generate xml like

<users>
   <user id=1>
          <email>abc</email>
   </user>
   <user id=2>
          <email>abc</email>
   </user>
</users>

Here I want to display id in each user tag. So How can I do that?

Thanks
Brijesh Shah
134ea397777886d6f0aa992672a50eaa?d=identicon&s=25 Mark Thomas (Guest)
on 2009-01-06 13:30
(Received via mailing list)
On Jan 6, 5:53 am, Brijesh Shah <rails-mailing-l...@andreas-s.net>
wrote:
>    </user>
> </users>
>
> Here I want to display id in each user tag. So How can I do that?

require 'builder'

builder = Builder::XmlMarkup.new(:indent => 2)

xml = builder.users do |users|
  (1..2).each do |user_id|
    users.user(:id => user_id) do |u|
      u.email("foo")
    end
  end
end

puts xml

#=>

<users>
  <user id="1">
    <email>foo</email>
  </user>
  <user id="2">
    <email>foo</email>
  </user>
</users>
Fde34ab22ac6ae44693f36b9f7fed41d?d=identicon&s=25 Brijesh Shah (brijesh)
on 2009-01-07 08:59
Thanks for the help
This topic is locked and can not be replied to.