How can i prefill datetime_select with times in cu

2019-02-19 01:55发布

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?

1条回答
甜甜的少女心
2楼-- · 2019-02-19 02:56

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.

查看更多
登录 后发表回答