Rails datetime_select与时区 [英] Rails datetime_select with time zone

查看:48
本文介绍了Rails datetime_select与时区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个简单的日历应用程序,并且遇到时区问题.对于许多不错的日期和时间助手,操作视图和活动记录肯定不能与时区配合使用.

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.

我有一个约会模型,其约会时间为datetime.

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

我当前的问题是在表单标签中使用datetime_select编辑约会时间.

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

使用UTC的当前日期和时间(而不是约会日期和时间)进行选择. appt_time 会被调用并返回针对时区调整的正确日期和时间.

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?

推荐答案

问题似乎出在您的select标签上,而不是时区偏移量上.看起来像你想要的

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 %>                                                                                                         

这篇关于Rails datetime_select与时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆