time_select 表单助手将新时间对象解释为 UTC 而不是配置的时区 [英] time_select form helper interprets new time object as UTC not as configured time zone

查看:85
本文介绍了time_select 表单助手将新时间对象解释为 UTC 而不是配置的时区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个对我来说似乎很简单的问题,但我不知道我做错了什么.在我的应用程序中,用户可以通过一个简单的 form_for 添加他们的课程.他们可以输入课程讲座的开始时间和结束时间,如下所示:

开始时间
<%= time_select :course, :start_time, { :minute_step =>5、:ampm =>真 } %>

<div class="field">结束时间<br/><%= time_select :course, :end_time, { :minute_step =>5、:ampm =>真 } %>

我在 application.rb 中将我的时区配置为东部时间,这似乎可以正常工作,因为 created_at 在正确的时区中返回.

但是,我遇到的问题是 start_time 和 end_time 作为 UTC 输入到数据库中.因此,当用户选择在上午 10 点开始上课时,输入的时间为 10 UTC 而不是 10 AM EST/15:00 UTC.我在这里缺少什么?看起来我想要发生的事情应该是可能的.

解决方案

总结评论中的答案以从未回答"过滤器中删除此问题:

蒂姆·布兰德斯推荐:

<块引用>

也许这对您有帮助:带 rails 3 的时区 ...我从未使用过time_select 但在处理时区时总是 datetime_select,也许这可能是一个问题,但似乎不太可能.

适用于 菲利普五世的解决方案:

<块引用>

我的 start_time 和 end_time 没有保存在时区内,而是保存为 UTC 时间....我将 start_time 和 end_time 的列类型更改为 datetime,这解决了我遇到的问题.我猜时间列只允许 UTC.

I am running into what to me seems like a simple issue, but I can't figure out what I am doing wrong. In my app users can add their course through a simple form_for. They can enter a start_time and end_time for the course's lectures, like so:

<div class="field">
  Start Time<br />
  <%= time_select :course, :start_time, { :minute_step => 5, :ampm => true } %>
</div>

<div class="field">
  End Time<br />
  <%= time_select :course, :end_time, { :minute_step => 5, :ampm => true } %>
</div>

I configured my time zone in application.rb to be set to Eastern Time and this seems to work correctly as created_at is returned in the right time zone.

However, the problem I am encountering is that start_time and end_time are entered as UTC into the database. So when a user selects a class to start at 10 AM it is entered as 10 UTC not 10 AM EST / 15:00 UTC. What am I missing here? It seems like what I want to happen should be possible.

解决方案

Summarizing the answer from the comments in order to remove this question from the "Unanswered" filter:

Suggested by Tim Brandes:

Maybe this helps you: Timezone with rails 3 ... I have never used a time_select but always a datetime_select when dealing with timezones, maybe this could be an issue, but it seems rather unlikely.

Solution that worked for Philip V:

My start_time and end_time are not being saved within the time zone but saved as a UTC time. ... I changed the column type for start_time and end_time to datetime and this fixed the issues I had. I guess time columns only allow UTC.

这篇关于time_select 表单助手将新时间对象解释为 UTC 而不是配置的时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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