2012-03-08 43 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