2012-12-18 35 views
0

如果我有一個日期時間類型的due列,我會如何調用.strftimeRails 3 - strftime

舉例來說,我有

<% @todos.each do |todo|%> 
    <%= todo.due.strftime("%h %d") %> 
<% end %> 
在我的節目鑑於

,我得到未定義的方法`strftime的」爲無:NilClass

我有這個在我的節目在控制器方法:

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

注:我在更新或創建時不試圖顯示,只想顯示格式化的日期。

回答

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. 
+0

BAM!那樣做了。現在....這是如何工作的? :) –

+0

請檢查表中的所有行是否有應有列的值。 – vijikumar

+0

並非所有行都具有適用列的值。 –