我希望自定义格式为:results
,因此我可以在观看中进行@obj.date.to_s(:results)
调用。
如果我将自定义格式放在初始化程序中,那么它可以工作:
Time::DATE_FORMATS[:results] = "%D"
但是,如果我尝试将其放在我的en.yml
文件中,那么它就不起作用(因为它不会按预期格式化它):
en:
time:
# See also config/initializers/time_formats.rb for lambda based configs
formats:
results: '%D'
date:
formats:
default: '%m/%d/%Y"'
results: '%D'
我读到的所有内容都表明这应该有效。我错过了什么?
编辑:这基本上是Rails not picking up custom date & time formats in en.yml的副本所以我想问题是如何在yml中设置默认值而不是初始化(更容易管理)。是否有内置方式,或者我是否需要手动加载yaml文件并自行设置Time::DATE_FORMAT
哈希?
答案 0 :(得分:1)
答案在Rails not picking up custom date & time formats in en.yml
你提到的第二种方式是使用I18n(国际化),这意味着你可以使用它来呼叫它。
在你的观点中你可以做到
<%= l @obj.date, format: :result %>