try this..
<% @todos.each do |todo|%>
<%= todo.due ? todo.due.strftime("%h %d") : nil %>
<% end %>
If the date is there then it will convert to the given format else it will display nil.
Question
If I have a :due column that is a datetime type, how would I make calling .strftime
on it work?
For instance, I have
<% @todos.each do |todo|%>
<%= todo.due.strftime("%h %d") %>
<% end %>
in my show view and I get undefined method `strftime' for nil:NilClass
I have this in my show method in the controller:
@todos = ProjectTodo.for_project(params[:id])
Note: I'm not trying to display when it was updated or created, just want to display a formatted date.
Solution
try this..
<% @todos.each do |todo|%>
<%= todo.due ? todo.due.strftime("%h %d") : nil %>
<% end %>
If the date is there then it will convert to the given format else it will display nil.