Rails datetime

编程入门 行业动态 更新时间:2024-10-27 04:37:21
本文介绍了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

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

发布评论

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

>www.elefans.com

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