Rails 3.2时间以前的单词方法返回html内容

时间:2013-04-13 22:15:23

标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-3.1 ruby-on-rails-3.2

我想用单词显示日期时间,比如“1小时前”,我尝试了以下方法,但它返回的字符串格式返回html,如下所示。

方法

helper.time_ago_in_words(Time.now)

结果

=> "<span class=\"translation_missing\" title=\"translation missing: en.less_than_x_minutes\">Less Than X Minutes</span>"

rails console output:

>> helper.time_ago_in_words(Time.now)
=> "<span class=\"translation_missing\" title=\"translation missing: en.less_than_x_minutes\">Less Than X Minutes</span>"
>> I18n.locale = :de
=> :de
>> helper.time_ago_in_words(Time.now)
=> "<span class=\"translation_missing\" title=\"translation missing: de.less_than_x_minutes\">Less Than X Minutes</span>"
>> I18n.locale = :en
=> :en
>> helper.time_ago_in_words(Time.now)
=> "<span class=\"translation_missing\" title=\"translation missing: en.less_than_x_minutes\">Less Than X Minutes</span>"

3 个答案:

答案 0 :(得分:1)

你在配置中使用任何语言环境吗?英语肯定是默认的,它应该可以正常工作。

2.0.0p0 :001 > helper.time_ago_in_words(Time.now)
 => "less than a minute" 
2.0.0p0 :002 > I18n.locale = :de
 => :de 
2.0.0p0 :003 > helper.time_ago_in_words(Time.now)
 => "translation missing: de.datetime.distance_in_words.less_than_x_minutes" 
2.0.0p0 :004 > I18n.locale = :en
 => :en 
2.0.0p0 :005 > helper.time_ago_in_words(Time.now)
 => "less than a minute" 

您是否可以尝试在helper对象上调用此方法,就像我在控制台(rails c)中所做的那样?什么是输出?

答案 1 :(得分:0)

在rails控制台中,如果我只使用ActionView :: Helpers中的time_ago_in_words,我将收到translation_missing错误消息。但如果我从ActionView :: Helpers :: DateHelper中包含它,它将按预期工作。

工作的:

[2] pry(main)> include ActionView::Helpers::DateHelper
=> Object 
[3] pry(main)> time_ago_in_words(Time.now)
=> "less than a minute"

翻译缺失:

[1] pry(main)> include ActionView::Helpers
=> Object [2] 
pry(main)> time_ago_in_words(Time.now)
=> "<span class=\"translation_missing\" title=\"translation missing: en.less_than_x_minutes\">Less Than X Minutes</span>"

答案 2 :(得分:0)

试试这个:

  

time_ago_in_words(Time.now)