Forum: Ruby on Rails Preventing nil data 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.
8a2cd6e09f7f0dc8f903dcef0ab197e5?d=identicon&s=25 Rock Roll (rocknrollcar)
on 2008-10-21 13:42
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? %>
8a2cd6e09f7f0dc8f903dcef0ab197e5?d=identicon&s=25 Rock Roll (rocknrollcar)
on 2008-10-21 13:44
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
C64e63b70be7dfed8b0742540b8b27e5?d=identicon&s=25 Mark Reginald James (Guest)
on 2008-10-22 00:06
(Received via mailing list)
Rock Roll 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
This topic is locked and can not be replied to.