ruby on rails - if a value from controller is nil, how do I display 0? -
i creating report looks number of emails sent during period of time:
i display in view follows:
<td><%= @emails_sent.size %></td>
and generated in controller follows:
@sent_emails = contactemail.all(:conditions => ['date_sent >= ? , date_sent <= ?', @monday, @friday])
but emails have been sent, makes nil, causes view bonk.
what way address "nil" when .find method comes nothing goes 0 instead of thinking 'nil?
when using @neutrino solution might give error (if select null) in view call
<%= @emails_sent.size %>
reason if selection nil, returns '0' , in view expecting array
you have 2 options
1 - modify @neutrino code slightly
@sent_emails = contactemail.all(:conditions => ['date_sent >= ? , date_sent <= ?', @monday, @friday]) || []
** note [] instead of 0
2 - count sql , rid of .size in view
cheers
sameera
update - changed array.new [] #thanks @gertas
Comments
Post a Comment