Rails datetime_select with time zone

拟墨画扇 提交于 2021-02-10 22:20:32

问题


I am writing a simple calendar app and having troubles with time zones. For as many nice Date and Time helpers, action view and active record sure don't play nice with time zones.

I have an appointment model with the appointment_time attribute which is a datetime.

My current issue is editing appointment_time with datetime_select within a form tag.

<%= datetime_select :appointment_time, @appt.appt_time,ampm: true %>

def appt_time
  appointment_time.in_time_zone(time_zone) #this is the appointment owners time zone
end

gives a select with the current date and time in UTC, not the appointment date and time. appt_time does get called and returns the correct date and time adjusted for the time zone.

The docs mention nothing about time zones, what is the best way to accomplish it?


回答1:


It looks like the problem is with your select tag, not the time zone offset. Looks like you want

<%= datetime_select "appointment", "appointment_time", default: @appt.appt_time, ampm: true %>                                                                                                         


来源:https://stackoverflow.com/questions/34098442/rails-datetime-select-with-time-zone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!