我如何预填充datetime

编程入门 行业动态 更新时间:2024-10-27 12:27:04
本文介绍了我如何预填充datetime_select与自定义时区的时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有不同时区的活动。

强>我想时间&

但是,当我点击编辑, 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

本文发布于:2023-10-17 22:18:15,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1502236.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:datetime

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!