2012-03-08 37 views
2

我使用它來獲取一個選擇的所有日期:的strftime爲options_from_collection_for_select

options_from_collection_for_select(@expiration_dates, :exp_date, :exp_date) 

這回我這個:

2010-10-21 14:01:38 +0200 
2010-12-19 12:01:18 +0200 

但是,我需要的是結果轉換成的格式「dm」,但是當我用

:exp_date.strftime("%d-%m") 

它顯然返回一個錯誤,因爲它是一個字符串對象,我把它當作時間對象。 如何轉換該值並顯示它?在模型中

options_from_collection_for_select(@expiration_dates, :exp_date, :exp_date_label) 

回答

2

如果你能使你的模型的方法,你可以把它在options_from_collection_for_select,所以

def exp_date_label 
    exp_date.strftime("%d-%m") 
end 
+0

你是我的英雄:-) – John 2012-03-08 18:43:27

+0

公頃!很高興我能幫上忙。 – miked 2012-03-08 20:44:35

+0

哇這是一個聰明的主意;) – Trip 2012-10-08 21:46:08