参考地址:
http://www.blogjava.net/fl1429/archive/2009/08/18/291678.html
1.在console控制台下执行:helper.distance_of_time_in_words(Time.now - 1.days, Time.now)
显示提示:"translation missing: zh.datetime.distance_in_words.x_days"
该提示告诉我们这个helper方法需要结合I18n的local文件,以显示对应的描述信息。本人在自己项目中的local文件做了如下配置:
#详细的键值,可参考Rails API中distance_of_time_in_words的源代码
datetime:
distance_in_words:
less_than_x_seconds: '不久前'
half_a_minute: '不久前'
less_than_x_minutes: '不久前'
x_minutes: '%{count}分钟前'
abount_x_hours: '%{count}小时前'
x_days: '%{count}天前'
abount_x_mouths: '%{count}月前'
x_mouths: '%{count}月前'
abount_x_years: '%{count}年前'
over_x_years: '%{count}年前'
almost_x_years: '%{count}年前'
这时候如果执行:helper.distance_of_time_in_words(Time.now - 1.days, Time.now), 就会提示:1天前
附: respond_to?方法,判断某个对象是否有某个方法可调用,如:
current_time = Time.now
current_time.respond_to?(:to_time) #若current_time可以调用to_time方法,则返回true,若不能,则返回false