# Preventing nil data in views

**URL:** <https://www.ruby-forum.com/t/preventing-nil-data-in-views/151441>\
**Category:** Rails\
**Created:** [October 21, 2008, 1:42pm UTC](https://www.ruby-forum.com/t/preventing-nil-data-in-views/151441 "2008-10-21T13:42:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rocknrollcar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f14d63/32.png) [@rocknrollcar](https://www.ruby-forum.com/u/rocknrollcar)\
**Post date:** [October 21, 2008, 1:42pm UTC](https://www.ruby-forum.com/t/preventing-nil-data-in-views/151441/1 "2008-10-21T13:42:48Z")

</div>

I have an activerecord model Student. In show action of Student  
controller i retrieve the database data about a Student and display it  
in the view. Student object has fields like age, gender, dateofbirth,  
address, etc where a few fields are optional. Now in my view i display  
like this

Name : \<%= @student.name %\>  
Age : \<%= @student.age %\>

I don’t want the optional field labels to get displayed. So i used

\<% if @student.address.nil? %\>

---

<div class="post-metadata">

**Author:** ![rocknrollcar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f14d63/32.png) [@rocknrollcar](https://www.ruby-forum.com/u/rocknrollcar)\
**Post date:** [October 21, 2008, 1:44pm UTC](https://www.ruby-forum.com/t/preventing-nil-data-in-views/151441/2 "2008-10-21T13:44:30Z")

</div>

Sorry for posting wrongly

I have an activerecord model Student. In show action of Student  
controller i retrieve the database data about a Student and display it  
in the view. Student object has fields like age, gender, dateofbirth,  
address, etc where a few fields are optional. Now in my view i display  
like this

Name : \<%= @student.name %\>  
Age : \<%= @student.age %\>

I don’t want the optional field labels to get displayed. So i used

\<% if @student.address.nil? %\>  
Address : \<%= @student.address %\>  
\<% end %\>

Does this method affect the performance of rendering the view?

Thank you

---

<div class="post-metadata">

**Author:** ![rocknrollcar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f14d63/32.png) [@rocknrollcar](https://www.ruby-forum.com/u/rocknrollcar)\
**Post date:** [October 22, 2008, 12:06am UTC](https://www.ruby-forum.com/t/preventing-nil-data-in-views/151441/3 "2008-10-22T00:06:21Z")

</div>

Rock R. wrote:

> I don’t want the optional field labels to get displayed. So i used
> 
> \<% if @student.address.nil? %\>  
> Address : \<%= @student.address %\>  
> \<% end %\>
> 
> Does this method affect the performance of rendering the view?

Only very slightly. It’s not worth worrying about.

By the way, you want to use an “unless” rather than an “if”.

You could speed it up slightly:

\<%= “Address : #{@student.address}” if @student.address %\>

–  
Rails Wheels - Find Plugins, List & Sell Plugins -  
[http://railswheels.com](http://railswheels.com)
