Formtastic的日期时间选择与用户特定的时区

时间:2011-11-01 04:39:45

标签: formtastic

我们有一个MongoDB事件记录集合,其中包含一个以UTC格式保存的scheduled_at日期时间字段。每个用户都有一个特定的时区,我们将其应用于前端的时间戳。

编辑给定的事件记录时,我无法弄清楚如何让formtastic的日期时间选择尊重用户的时区。代码段如下所示:

f.input :scheduled_at, :as => :datetime, :input_html => { :value => event.scheduled_at.in_time_zone(Time.zone) }

看起来我不能以这种方式覆盖字段的值; scheduled_at下拉列表仍然只显示UTC,而不是正确的Time.zone。有谁知道这是正确的方法是什么?

1 个答案:

答案 0 :(得分:0)

在我的情况下,我需要一个DateSelectInput来显示UTC时间(长篇故事,传统支持)。执行此操作的唯一方法是创建一个继承自DateTimeSelectInput的输入(我的DateSelectInput)。这是因为Timeish直接从表单对象获取值:https://github.com/justinfrench/formtastic/blob/3.1.3/lib/formtastic/inputs/base/timeish.rb#L159(至少在最新版本中)

这是我的代码,但你得到了要点,只需更改值并将其自身呈现为父类,以便所有CSS和其他工作按预期工作。

class UtcDateSelectInput < Formtastic::Inputs::DateSelectInput
  def value
     super.utc if super.respond_to? :utc
   end

  def as
    'date_select'
  end
end
相关问题