Forum: Ruby on Rails Stumped on RXML Template

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.
21f7ed21f11a809050594c82eab11d67?d=identicon&s=25 Robert Walker (Guest)
on 2007-04-18 08:29
(Received via mailing list)
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.
8310c5a7c769345114597bcdef111488?d=identicon&s=25 Ben Munat (Guest)
on 2007-04-18 09:42
(Received via mailing list)
Robert Walker 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
This topic is locked and can not be replied to.