问题描述
我有不同时区的活动。
强>我想时间&
但是,当我点击编辑, datetime_select
总是显示用户时区的时间(而不是事件的时间)。
示例: / p>
- 从阿姆斯特丹上午10点开始的活动(GMT + 1)
- 用户时区设定为伦敦(GMT + 0)
结果:编辑时事件时间错误预设为上午9时 p>
代码段:
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 包含所需的时区(上例中的Amsterdam)。
我如何可以有 datetime_select
预设为编辑时各个区域中的事件时间
pixeltrix 在错误报告的线程中指出,它更清晰地覆盖有问题的属性的读者/ getter:
#在事件模型中
def beginn
super.in_time_zone(time_zone)if super
end
def endd
super。 in_time_zone(time_zone)if super
end
行动可以省略,并避免与依赖 Time.zone 的其他部分发生干扰。
I have events that can be in different time zones.
Upon edit I want the time & date to show with the time zone of that very event.
However, when I hit edit, datetime_select
always shows the time of the users time zone (as opposed to the one of the event).
Example:
- Event starting at 10 a.m. in Amsterdam (GMT+1)
- Users time zone configured as London (GMT+0)
Result: Upon edit the event time is falsely preset to 9 a.m.
Code snippet:
def edit
Time.zone = @event.time_zone
@event.beginn = @event.beginn.in_time_zone
@event.endd = @event.endd.in_time_zone
# [...]
end
Note that @event.time_zone contains the desired time zone ("Amsterdam" in the above example).
How can I have datetime_select
preset to the events time in it's respective zone upon edit?
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
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与自定义时区的时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!