# Stumped on RXML Template

**URL:** <https://www.ruby-forum.com/t/stumped-on-rxml-template/92321>\
**Category:** Rails\
**Created:** [April 18, 2007, 8:29am UTC](https://www.ruby-forum.com/t/stumped-on-rxml-template/92321 "2007-04-18T08:29:06Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![rlwinter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ecccb3/32.png) [@rlwinter](https://www.ruby-forum.com/u/rlwinter)\
**Post date:** [April 18, 2007, 8:29am UTC](https://www.ruby-forum.com/t/stumped-on-rxml-template/92321/1 "2007-04-18T08:29:06Z")

</div>

Hi all,

I have a bit of a problem and don’t know how to proceed:

I have the following RXML Template that:

* * *

## xml.instruct! xml.articles do for article in @articles xml.article do xml.id(article.id, :type =\> ‘integer’) xml.created\_at(article.created\_at.xmlschema, :type =\> ‘datetime’) xml.title(article.title) xml.author do xml.id(author.id, :type =\> ‘integer’) xml.first\_name(author.first\_name) xml.last\_name(author.last\_name) end end end end

Some articles have an author and some do not. In this case I get an  
error complaining about author being null when accessing author.id.

I can’t figure out how to conditionally include the author tag in the  
XML only if article.author is not nil.

---

<div class="post-metadata">

**Author:** ![rlwinter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ecccb3/32.png) [@rlwinter](https://www.ruby-forum.com/u/rlwinter)\
**Post date:** [April 18, 2007, 9:42am UTC](https://www.ruby-forum.com/t/stumped-on-rxml-template/92321/2 "2007-04-18T09:42:10Z")

</div>

Robert W. wrote:

> ```
> xml.first_name(author.first_name)
> xml.last_name(author.last_name)
> end
> end
> 
> ```
> 
> end  
> end
> 
> I can’t figure out how to conditionally include the author tag in the  
> XML only if article.author is not nil.

Try

…  
xml.author do  
xml.id(article.author.id, :type =\> ‘integer’)  
xml.first\_name(article.author.first\_name)  
xml.last\_name(article.author.last\_name)  
end if article.author  
…

b
