如何使用自定义时区中的时间预填充 datetime_select? [英] How can i prefill datetime_select with times in custom time zone?

查看:34
本文介绍了如何使用自定义时区中的时间预填充 datetime_select?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有活动可以在不同时区.

编辑我想要时间和;日期与该事件的时区一起显示.

Upon edit I want the time & date to show with the time zone of that very event.

但是,当我点击 edit 时,datetime_select 总是显示用户时区的时间(而不是事件之一).

However, when I hit edit, datetime_select always shows the time of the users time zone (as opposed to the one of the event).

示例:

  • 活动于上午 10 点在阿姆斯特丹开始(GMT+1)
  • 用户时区配置为伦敦 (GMT+0)

结果:编辑时事件时间被错误地预设为上午 9 点

代码片段:

def edit
  Time.zone = @event.time_zone
  @event.beginn = @event.beginn.in_time_zone
  @event.endd = @event.endd.in_time_zone

  # [...]
end

请注意,@event.time_zone 包含所需的时区(上例中的阿姆斯特丹").

Note that @event.time_zone contains the desired time zone ("Amsterdam" in the above example).

如何在编辑时将 datetime_select 预设为其各自区域中的事件时间?

How can I have datetime_select preset to the events time in it's respective zone upon edit?

推荐答案

正如 pixeltrix 在错误报告的线程中指出的那样,覆盖相关属性的读取器/获取器更干净,例如所以:

As pixeltrix pointed out in the thread of the bug report, it's cleaner to override the readers/getters of the attributes in question like so:

# in event model

def beginn
  super.in_time_zone(time_zone) if super
end

def endd
    super.in_time_zone(time_zone) if super
end

这样可以省略问题中概述的编辑操作中的逻辑,并避免与依赖Time.zone的其他部分发生干扰.

This way the logic in the edit action as outlined in the question can be omitted and interferences with other parts that rely on Time.zone are avoided.

这篇关于如何使用自定义时区中的时间预填充 datetime_select?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆