在 Rails collection_select 中使用短日期时间

Dre*_*uzz 3 time select ruby-on-rails short

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

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

f.collection_select :appointment_id, @available, :id, :time, {:prompt => "Select a time"}
Run Code Online (Sandbox Code Playgroud)

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

appointment.time.to_s(:short)
Run Code Online (Sandbox Code Playgroud)

显示为 31 Oct 08:00

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

jdl*_*jdl 5

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

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

def short_time
  time.to_s(:short)
end
Run Code Online (Sandbox Code Playgroud)

然后用作:short_time中的 text_method 符号collection_select