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.
Brijesh S. (Guest)
on 2009-01-06 12: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 S.
Mark T. (Guest)
on 2009-01-06 14:30
(Received via mailing list)
On Jan 6, 5:53 am, Brijesh S. <removed_email_address@domain.invalid>
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>
Brijesh S. (Guest)
on 2009-01-07 09:59
Thanks for the help
This topic is locked and can not be replied to.