3

这是一个小烦恼,我找不到任何东西。

我在 Appointment 对象上有一个集合选择,它显示了可用约会的时间:

f.collection_select :appointment_id, @available, :id, :time, {:prompt => "Select a time"}

这使用标准 DateTime 格式显示:2011-10-31 08:00:00 UTC 但我真的更喜欢短格式,如下所示:

appointment.time.to_s(:short)

显示为 10 月 31 日 08:00

使用符号时,我似乎找不到语法。有人知道这个吗?

4

1 回答 1

5

[虽然如果你想保持 MVC 的纯度,你可以跳过一些障碍(格式化日期可能不是模型应该担心的),但我认为这很简单,可以。]

向您的模型添加一个方法,为您提供想要查看的字符串。

def short_time
  time.to_s(:short)
end

然后:short_time用作您的 text_method 符号在collection_select.

于 2011-10-20T03:59:37.573 回答