Ruby on Rails Friday, October 26, 2012

Dave,

You nailed it. Either way works fine. I could not find a reference
with the grammar explained as you do below, at least not in the specific
XML Builder context.

Thanks.
Page
-----Original Message-----
From: rubyonrails-talk@googlegroups.com
[mailto:rubyonrails-talk@googlegroups.com] On Behalf Of Dave Aronson
Sent: Thursday, October 25, 2012 8:02 PM
To: rubyonrails-talk@googlegroups.com
Subject: Re: [Rails] XML Builder

On Thu, Oct 25, 2012 at 10:26 PM, bwb <bwbasheer@retrievalsystems.com>
wrote:

> Using XML Builder, or some other facility, how does one emit an
> element instance that has both attributes and non-empty content?

Long story short: pass it the content, and then the attributes. (Or the
other way around, but then I think you'd have to put the attributes in
braces (since it's a hash and you'll have stuff after it), and put
parens on the call (so Ruby doesn't think the hash is a
block).)

In your case, if I correctly spotted the difference between the current
and desired outputs, what I think you want would be:

xml.eotype EoType.find(official.eotypeID).name,
eotypeID: official.eotypeID
xml.source SourceType.find(official.sourceID).name,
sourceID: official.sourceID

Or, to reduce unclear redundancy:

eo_id = official.eotypeID
xml.eotype EoType.find(eo_id).name, eotypeID: eo_id
src_id = official.sourceID
xml.source SourceType.find(src_id).name, sourceID: src_id

Or, with the attributes hash first:

eo_id = official.eotypeID
xml.eotype({ eotypeID: eo_id }, EoType.find(eo_id).name)
src_id = official.sourceID
xml.source({ sourceID: src_id }, SourceType.find(src_id).name)

Try any of these (I'd favor #2) and let me know if it gets you what you
want.

-Dave

--
Dave Aronson, the T. Rex of Codosaurus LLC, secret-cleared freelance
software developer taking contracts in or near NoVa or remote.
See information at http://www.Codosaur.us/.

--
You received this message because you are subscribed to the Google
Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

No comments:

Post a Comment