In my view, I do this:
在我看来,我这样做:
<td><%= client.last_contact.to_date %></td>
That outputs this:
这输出:
2011-08-11
But when I do it in the console, I get the format I want:
但是当我在控制台中执行此操作时,我得到了我想要的格式:
1.9.3p194 :093 > c.last_contact
=> Thu, 11 Aug 2011 00:00:00 UTC +00:00
1.9.3p194 :094 > c.last_contact.to_date
=> Thu, 11 Aug 2011
In my view, I want it to display the day and the format that it does in the console.
在我看来,我希望它显示它在控制台中的日期和格式。
How do I achieve that?
我如何实现这一目标?
Thanks.
2 个解决方案
#1
6
The best way to display time/date in rails views is using I18n localization feature. You can set datetime formats in your config/locales
directory in yaml file. Something like this
在rails视图中显示时间/日期的最佳方法是使用I18n本地化功能。您可以在yaml文件的config / locales目录中设置日期时间格式。像这样的东西
# config/locales/datetime.en.yml
en:
time:
formats:
# Use the strftime parameters for formats.
# When no format has been given, it uses default.
# You can provide other formats here if you like!
default: "%d.%m.%Y"
short: "%d %b"
long: "%d %B %Y"
date: "%a, %d %b %Y"
Then use l
helper in your views
然后在视图中使用l helper
<%= l client.last_contact, :format => :date %>
- Rails guides: http://guides.rubyonrails.org/i18n.html#adding-date-time-formats
- Possible formats: https://github.com/svenfuchs/rails-i18n/blob/master/rails/locale/en.yml
- Datetime in ruby: http://www.dzone.com/snippets/date-time-format-ruby
Rails指南:http://guides.rubyonrails.org/i18n.html#adding-date-time-formats
可能的格式:https://github.com/svenfuchs/rails-i18n/blob/master/rails/locale/en.yml
ruby中的日期时间:http://www.dzone.com/snippets/date-time-format-ruby
#2
5
The console calls inspect
on values whereas erb tags call to_s
on their value.
控制台调用值来检查值,而erb标签调用值的on_s。
While you could call inspect
in your view, I'd encourage you to either use strftime
which allows you to define precisely what you want
虽然您可以在视图中调用inspect,但我建议您使用strftime来准确定义您想要的内容
Date::today.strftime('%a, %d %b %Y')`
or use the I18n.localize
helper (aliased to l
) which allows you to define formats and reuse them
或使用I18n.localize助手(别名为l),它允许您定义格式并重复使用它们
I18n.localize Date::today, :format => :long
#1
6
The best way to display time/date in rails views is using I18n localization feature. You can set datetime formats in your config/locales
directory in yaml file. Something like this
在rails视图中显示时间/日期的最佳方法是使用I18n本地化功能。您可以在yaml文件的config / locales目录中设置日期时间格式。像这样的东西
# config/locales/datetime.en.yml
en:
time:
formats:
# Use the strftime parameters for formats.
# When no format has been given, it uses default.
# You can provide other formats here if you like!
default: "%d.%m.%Y"
short: "%d %b"
long: "%d %B %Y"
date: "%a, %d %b %Y"
Then use l
helper in your views
然后在视图中使用l helper
<%= l client.last_contact, :format => :date %>
- Rails guides: http://guides.rubyonrails.org/i18n.html#adding-date-time-formats
- Possible formats: https://github.com/svenfuchs/rails-i18n/blob/master/rails/locale/en.yml
- Datetime in ruby: http://www.dzone.com/snippets/date-time-format-ruby
Rails指南:http://guides.rubyonrails.org/i18n.html#adding-date-time-formats
可能的格式:https://github.com/svenfuchs/rails-i18n/blob/master/rails/locale/en.yml
ruby中的日期时间:http://www.dzone.com/snippets/date-time-format-ruby
#2
5
The console calls inspect
on values whereas erb tags call to_s
on their value.
控制台调用值来检查值,而erb标签调用值的on_s。
While you could call inspect
in your view, I'd encourage you to either use strftime
which allows you to define precisely what you want
虽然您可以在视图中调用inspect,但我建议您使用strftime来准确定义您想要的内容
Date::today.strftime('%a, %d %b %Y')`
or use the I18n.localize
helper (aliased to l
) which allows you to define formats and reuse them
或使用I18n.localize助手(别名为l),它允许您定义格式并重复使用它们
I18n.localize Date::today, :format => :long