我有不同时区的活动。
强>我想时间&
但是,当我点击编辑, 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 # [...] endNote 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 endThis 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
发布评论