rails之distance_of_time_in_words等显示时间差大概描述的方法

参考地址:

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值