Forum: Ruby on Rails checking for existence of object in views

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.
212f29023be1ec193f493aca45181d71?d=identicon&s=25 Mike Bailey (Guest)
on 2006-03-07 01:07
(Received via mailing list)
In my views I often find an error caused by trying to display a
property of a non-existent object.

Is there is a simpler way to avoid this than to append an 'if'
conditional at the end
of each statement where the 'belongs_to' object does not exist?

<% for person in @people %>
   <tr>
     <td><%=h person.address.streetname if person.address %></td>
   </tr>
<% end %>

thanks,

Mike
This topic is locked and can not be replied to.