填充“/edit”的表单字段时,Rails 应用程序 config.time_zone 不适用看法
我在 Rails 应用程序中指定了 config.time_zone,但表单字段中检索到的时间仍然呈现为 UTC(这会在更新时产生问题)。难道不应该将其转换为指定区域的当地时间吗?
/config/application.rb(仅相关行):
module ExampleSite
class Application < Rails::Application
config.time_zone = 'Central Time (US & Canada)'
end
end
/events/edit.html.erb(完整文件):
<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(仅相关行:)
<div class="field">
<%= f.label :time_start, "Start Time" %><br />
<%= f.text_field :time_start, :class => "datetimefield" %>
</div>
<div class="field">
<%= f.label :time_end, "End Time (if applicable)" %><br />
<%= f.text_field :time_end, :class => "datetimefield" %>
</div>
当我输入日期时间字符串来创建新事件时,该值已正确保存(以 UTC 为单位),并根据需要在我的视图中呈现(以本地时区),在 config.time_zone 切换之前已呈现 UTC (所以我知道已经进行了切换)。
但是,当我去编辑事件的任何其他属性时,呈现到 /edit 视图中表单字段的时间是 UTC 时间 - 这意味着当我更新事件时,时间会重新保存,就好像时间已经已重新输入并假定为本地时间,这会将时间移动 5 小时(我的本地时间与 UTC 的差异),因为系统将“更新的”时间属性转换为 UTC 进行存储。
如何在表单字段中呈现本地化时间?
运行 Rails 3.0.5,部署到 Heroku(尽管开发和生产环境中都存在问题)
I specified config.time_zone in my Rails app, but the retrieved times in form fields still render as the UTC (which creates problems on updates). Shouldn't this be converted to local time in the specified zone?
/config/application.rb (relevant lines only):
module ExampleSite
class Application < Rails::Application
config.time_zone = 'Central Time (US & Canada)'
end
end
/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 (relevant lines only:)
<div class="field">
<%= f.label :time_start, "Start Time" %><br />
<%= f.text_field :time_start, :class => "datetimefield" %>
</div>
<div class="field">
<%= f.label :time_end, "End Time (if applicable)" %><br />
<%= f.text_field :time_end, :class => "datetimefield" %>
</div>
When I enter the datetime string to create a new event, the value is saved properly (in UTC) and rendered in my views as desired (in the local time zone) where it had been rendering UTC before the config.time_zone switch (so I know the switch was made).
But when I go to edit any other attribute of the event, the time rendered to the form field in the /edit view is the UTC time--which means when I update the event, the time is re-saved as though the time had been re-entered and presumed local, which shifts the time by 5 hours (my local difference from UTC) as the system converts the "updated" time attribute to UTC for storage.
How can I make the localized time be rendered in my form fields?
Running Rails 3.0.5, deploying to Heroku (though the problem exists in both development and production environments)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
事实证明,真正的问题在于文本字段本身。
我的“config.time_zone”设置在“index”和“show”视图中工作得很好(没有任何额外的方法或黑客),并且它在“edit”视图中也工作,只要我使用 datetime_select 代替一个文本字段的。
由于这对我来说不是一个选择(我使用的是 jQuery UI 的 DatePicker,它需要一个 text_field),我调查了 text_field 特定问题并 回答了我自己关于该主题的另一个 StackOverflow 问题。
如果您遇到 text_field 问题,请查看该问题/答案。
It turns out the real problem was in the text_fields themselves.
My 'config.time_zone' setting was working just fine (without any extra methods or hacks) in my 'index' and 'show' views, and it worked in the 'edit' view, too, as long as I used a datetime_select instead of a text_field.
As this wasn't an option for me (I was using jQuery UI's DatePicker, which needs a text_field), I investigated the text_field specific problem and answered another StackOverflow question of my own on the subject.
If you're having the text_field problem, check out that question/answer.
只需使用这个:
我为此创建了一个小应用程序并且它有效。
Just use this:
I create a little app for this and it's works.
你可以这样做:
这应该可以解决你在 Heroku 上的问题。
You can do something like this:
That should fix your issue on Heroku.