ruby on rails - Using strftime on nil object - how to prevent error?

ID : 274379

viewed : 24

Tags : ruby-on-railsstrftimeruby-on-rails





Top 2 Answer for ruby on rails - Using strftime on nil object - how to prevent error?

vote vote

99

One trick is to use try.

<%= text_field_tag :event_date, @event.event_date.try(:strftime, "%m/%d/%Y at %I:%M%p"), :size=>30 %> 

Or just check if event_date is set:

<%= text_field_tag :event_date, (@event.event_date && @event.event_date.strftime("%m/%d/%Y at %I:%M%p")), :size=>30 %> 
vote vote

83

Top 3 video Explaining ruby on rails - Using strftime on nil object - how to prevent error?







Related QUESTION?