应用程序显示错误的小时和分钟

发布于 2025-01-08 20:49:17 字数 1466 浏览 2 评论 0原文

首先:这不是错误区域的问题。在 irb 和数据库中一切都很好。当我想在视图中显示日期(created_at、updated_at 以及所有由我自己在每个模型中定义的日期)时,就会出现问题。我试图在 application.rb 中设置时区并从初始化程序中删除时间格式,但这并没有解决我的问题。

Annotate gem 生成的架构信息:

#  created_at                      :datetime
#  updated_at                      :datetime
#  publish_at                      :datetime

来自 irb:

1.9.2-p290 :004 > Time.zone
 => (GMT+00:00) UTC 
1.9.2-p290 :005 > Time.zone.now
 => Fri, 24 Feb 2012 12:14:04 UTC +00:00 
1.9.2-p290 :006 > Time.now
 => 2012-02-24 13:14:07 +0100

示例:

1.9.2-p290 :007 > Article.last
  Article Load (0.3ms)  SELECT `articles`.* FROM `articles` ORDER BY `articles`.`id` DESC LIMIT 1
 => #<... created_at: "2012-02-24 12:04:24", updated_at: "2012-02-24 12:04:24", publish_at: "2012-02-24 12:04:24"...> 

应用显示:

Created_at 2012-02-24 12:02, 更新于 2012-02-24 12:02, Publish_at 2012-02-24 12:02,

1.9.2-p290 :008 > Article.first
  Article Load (0.5ms)  SELECT `articles`.* FROM `articles` LIMIT 1
=> #<...created_at: "2012-01-30 10:28:07", updated_at: "2012-02-08 17:20:41", publish_at: "2012-02-08 17:20:33"...> 

应用程序显示:

Created_at 2012-01-30 10:01, 更新于 2012-02-08 17:02, Publish_at 2012-02-08 17:02

这适用于整个应用程序(包括active_admin)

抱歉我的英语;)有什么想法我的问题的原因是什么?

At first: thats not issue of wrong zone. In irb and database everything is fine. Problem occurs when I want to display dates (created_at, updated_at and all defined by myself in every model) in my views. I was trying to set time zone in application.rb and remove time formats from initializers and that didn't solved my issue.

schema information generated by Annotate gem:

#  created_at                      :datetime
#  updated_at                      :datetime
#  publish_at                      :datetime

from irb:

1.9.2-p290 :004 > Time.zone
 => (GMT+00:00) UTC 
1.9.2-p290 :005 > Time.zone.now
 => Fri, 24 Feb 2012 12:14:04 UTC +00:00 
1.9.2-p290 :006 > Time.now
 => 2012-02-24 13:14:07 +0100

Examples:

1.9.2-p290 :007 > Article.last
  Article Load (0.3ms)  SELECT `articles`.* FROM `articles` ORDER BY `articles`.`id` DESC LIMIT 1
 => #<... created_at: "2012-02-24 12:04:24", updated_at: "2012-02-24 12:04:24", publish_at: "2012-02-24 12:04:24"...> 

App displays:

Created_at 2012-02-24 12:02,
Updated_at 2012-02-24 12:02,
Publish_at 2012-02-24 12:02,

1.9.2-p290 :008 > Article.first
  Article Load (0.5ms)  SELECT `articles`.* FROM `articles` LIMIT 1
=> #<...created_at: "2012-01-30 10:28:07", updated_at: "2012-02-08 17:20:41", publish_at: "2012-02-08 17:20:33"...> 

App displays:

Created_at 2012-01-30 10:01,
Updated_at 2012-02-08 17:02,
Publish_at 2012-02-08 17:02

this applies whole app (including active_admin)

Sorry for my English ;) Any ideas what is the reason of my problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

黒涩兲箜 2025-01-15 20:49:17

我明白了:)这很愚蠢,但也许有人需要提醒:应用程序首先从语言环境 yml 文件获取格式(这是在活动管理中设置日期格式的唯一方法;初始化程序的格式并不重要):

time:
  formats:
    default: "%Y-%m-%d"
    short: "%b %d"
    long: "%Y-%m-%d %H:%m"

和 ofc 分钟长格式应为 %M (而不是 %m)

I've got it :) That was silly, but maybe someone will need reminder: App gets format from locale yml file first (thats only way to set date format in active admin; formats from initializers doesn't matter):

time:
  formats:
    default: "%Y-%m-%d"
    short: "%b %d"
    long: "%Y-%m-%d %H:%m"

and ofc minutes in long format should be %M (not %m)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文