Rails'config.time_zone'不适用于加载到text_field的日期时间数据。固定? [英] Rails 'config.time_zone' doesn't apply to datetime data loaded into a text_field. Fix?

查看:83
本文介绍了Rails'config.time_zone'不适用于加载到text_field的日期时间数据。固定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在我的表单中使用datetime_select时,Rails将会正确地调整日期时间数据的时区(按照我的 config.time_zone 设置),并且会正确地呈现在编辑视图中的时间。

When I use a datetime_select in my form, Rails will correctly adjust the timezone of the datetime data (as per my config.time_zone setting), and will render the properly-zoned time in the edit view of that same form.

但是当我尝试将相同的数据检索到一个text_field(相同的编辑视图),Rails似乎不适合时区。

But when I try to retrieve that same data into a text_field (for the same edit view of the same form), Rails doesn't seem to adjust for time zone.

/events/edit.html.erb(完整文件):

/events/edit.html.erb (full file):

<h1>Edit This Event</h1>
<%= form_for(@event) do |f| %>
    <%= render 'fields', :f => f %>
    <div class="actions">
        <%= f.submit "Update Event" %>
    </div>
<% end %>

/events/_fields.html.erb(仅限相关行):

/events/_fields.html.erb (relevant lines only:)

<div class="field">
    <%= f.label      :time_start, "Start Time" %><br />
    <%= f.text_field  :time_start, :class => "datetimefield" %>
</div>

示例:我的区域的偏移量为-500。我输入8:00作为start_time的形式,13:00 UTC被写入数据库,8:00呈现在 show 页面(感谢 config.time_zone 计算偏移量),但是当我尝试编辑该对象时,start_time的表单的text_field将加载13:00(如果提交未更改,则变为18: 00,当它存储在数据库中时)。

Example: My zone's offset is -500. I enter 8:00 as the start_time in the form, 13:00 UTC is written to the database, 8:00 is rendered on the show page (thanks to config.time_zone accounting for the offset), but when I try to edit that object, the form's text_field for the start_time is loaded with 13:00 (which, if submitted unaltered, becomes 18:00 when it's stored in the database).

这是有问题的,因为我试图使用jQuery UI的DatePicker(使用TimePicker附加组件),它依赖于text_field(不能使用 datetime_select )。

This is problematic, because I'm trying to use jQuery UI's DatePicker (with a TimePicker add-on), which relies on the text_field (won't work with a datetime_select).

有没有办法让Rails应用 config.time_zone 偏移到文本字段?

Is there a way to make Rails apply the config.time_zone offset to text fields?

运行Rails 3.0.5,部署到Heroku

提前道歉,要求一个非常类似的问题几周前。这个信息仍然适用,我现在就更好地理解这个问题,所以我重新询问。

Apologies in advance for having asked a very similar question a few weeks ago. The information there still applies, I just have a better understanding of the problem now, so I re-asked.

推荐答案

p>正如我在问题中所说的,我遇到的主要问题是让ActiveRecord将现有的config.time_zone设置应用于text_fields。

As I said in the question, the main problem I had was getting ActiveRecord to apply the existing 'config.time_zone' setting to text_fields.

事实证明,一个非常简单和面向未来的方式来实现这一点:只需明确添加一个值,并提取完全相同的数据 - 无需额外/新方法调用!在我的情况下,这意味着:

It turns out that there's a very simple and future-proof way to make that happen: just explicitly add a :value and pull the exact same data--no extra/new methods to call! In my case, that meant:

<%= f.text_field  :time_start, :value => f.object.time_start, :class => "datetimefield" %>

这是我要尝试的另一个潜在解决方案的中间步骤,我很惊讶地发现它完全适用于所有(再次,使用'config.time_zone'我已经设置在'config / application.rb')。

This was an intermediary step to another potential solution I was going to try, and I was surprised to find that it worked all on its own (again, working with the 'config.time_zone' I'd already set in 'config/application.rb').

ve测试了它,并且它适用时也适用适当的DST偏移量(就像预期的那样,config.time_zone设置)。

I've tested it, and it applies the proper DST offset when applicable as well (just as one would expect, since 'config.time_zone' is set).

这篇关于Rails'config.time_zone'不适用于加载到text_field的日期时间数据。固定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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