Rails 3 中时间格式规则应该放在哪里?

发布于 2024-09-18 03:57:05 字数 282 浏览 5 评论 0原文

我发现自己重复输入许多我定义的 strftime 。

观看 Ryan Bates 的railscasts ep 32/33(我认为)后,我为 to_s 方法创建了一个自定义选项,如 Time.now.to_s 中所示,这样我就可以执行 Time.now.to_s(:sw),其中 :sw 是我的自定义方法,例如检索“2010 年 9 月 23 日下午 5:00”。

但问题是,我不知道把#sw 的定义放在哪里。它应该位于初始化程序文件夹中的文件中吗?或者应该放在application.rb中?

谢谢!

I am finding myself repeating typing many strftime which I defined.

Having watch Ryan Bates's railscasts ep 32/33( I think), I created a custom option for the to_s method as in Time.now.to_s, so that I can do Time.now.to_s(:sw), where :sw is my custom method, to retrieve "23 Sep 2010, 5:00PM" for example.

But the problem is, I don't know where to put #sw's definition. Should it be in a file in in the initializer folder? Or should it go in application.rb?

Thanks!

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

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

发布评论

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

评论(4

望喜 2024-09-25 03:57:05

在语言环境文件中使用“时间”而不是“日期”,因为 Rails 时间戳是日期时间。

config/locales/en.yml

en:
  time:
    formats:
      default: "%Y/%m/%d"
      short: "%b %d"
      long: "%B %d, %Y"

app/views/posts/show.html.haml 中的

  = l post.updated_at
  = l post.created_at, :format => :long

Use "time" instead of "date" in your locales file, since Rails timestamps are datetimes.

in config/locales/en.yml

en:
  time:
    formats:
      default: "%Y/%m/%d"
      short: "%b %d"
      long: "%B %d, %Y"

in app/views/posts/show.html.haml

  = l post.updated_at
  = l post.created_at, :format => :long
对你再特殊 2024-09-25 03:57:05

我有一个文件 config/initialisers/time_formats.rb 包含:

...
Time::DATE_FORMATS[:posts] = "%B %d, %Y"
Time::DATE_FORMATS[:published] = "%B %Y"
...

您只需重新启动服务器即可让更改生效。

I have a file config/initialisers/time_formats.rb containing:

...
Time::DATE_FORMATS[:posts] = "%B %d, %Y"
Time::DATE_FORMATS[:published] = "%B %Y"
...

You just need to restart your server to have the changes picked up.

秋叶绚丽 2024-09-25 03:57:05

使用 Rails I18n API。

# config/locales/en.yml
en:
  date:
    formats:
      default: "%Y-%m-%d"
      short: "%b %d"
      long: "%B %d, %Y"

# in views 
= l post.updated_at # will use default format of date in locales yml file

请参阅I18n API

Use Rails I18n API.

# config/locales/en.yml
en:
  date:
    formats:
      default: "%Y-%m-%d"
      short: "%b %d"
      long: "%B %d, %Y"

# in views 
= l post.updated_at # will use default format of date in locales yml file

see about I18n API

风吹过旳痕迹 2024-09-25 03:57:05

请阅读这篇文章:

Rails - to_formatted_s

创建名为:config/ 的文件initializers/time_formats.rb

Time::DATE_FORMATS[:my_custom_time_format] = "%Y-%m-%d %H:%M"

你可以使用:

formated_date = my_date.to_formatted_s(:my_custom_time_format)

注意:你必须重新启动你的rails服务器(WEBRick、FCGI等)

Please read this post:

Rails - to_formatted_s

Create file with name: config/initializers/time_formats.rb

Time::DATE_FORMATS[:my_custom_time_format] = "%Y-%m-%d %H:%M"

And you can use:

formated_date = my_date.to_formatted_s(:my_custom_time_format)

Note: You must to restart your rails server (WEBRick, FCGI, etc)

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