Rails 3 - strftime

时间:2012-12-18 04:26:56

标签: ruby-on-rails strftime

如果我有一个日期时间类型的:due列,我如何在其上调用.strftime

例如,我有

<% @todos.each do |todo|%>
   <%= todo.due.strftime("%h %d") %>
<% end %>

在我的show视图中,我得到未定义的方法`strftime'为nil:NilClass

我在控制器的show方法中有这个:

@todos = ProjectTodo.for_project(params[:id])

注意:我没有尝试在更新或创建时显示,只是想显示格式化的日期。

1 个答案:

答案 0 :(得分:1)

试试这个..

 <% @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.