【问题标题】:Use short datetime in rails collection_select在 rails collection_select 中使用短日期时间
【发布时间】:2011-10-20 03:15:17
【问题描述】:

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

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

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

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

appointment.time.to_s(:short)

显示为 10 月 31 日 08:00

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

【问题讨论】:

    标签: ruby-on-rails select time short


    【解决方案1】:

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

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

    def short_time
      time.to_s(:short)
    end
    

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

    【讨论】:

    • 工作就像一个魅力。谢谢!
    • 您应该“接受”这个答案以表明它解决了您的问题(并将您的问题从“未回答”列表中删除)。您可以通过单击左侧的“勾号”来执行此操作。 :)
    猜你喜欢
    • 2010-11-18
    • 1970-01-01
    • 2014-01-02
    • 2013-11-07
    • 2012-05-10
    • 2013-05-18
    • 1970-01-01
    • 1970-01-01
    • 2023-03-17
    相关资源
    最近更新 更多