我如何制作一个'weeks_ago`帮助器?

时间:2010-03-28 22:40:19

标签: ruby-on-rails

我如何制作一个帮助器,告诉我几周前(向下舍入)使用导轨?它可以基于time_ago_in_words帮助程序,但是我希望它返回:“上周”,然后是two weeks agothree weeks ago等......

3 个答案:

答案 0 :(得分:4)

试试这个:

def my_time_ago_in_words(from_time, include_seconds = false)
  to_time   = Time.now
  weeks_ago = ((to_time - from_time)/1.week).abs
  [nil, "last week", "two weeks ago", "three weeks ago"][weeks_ago] || 
      distance_of_time_in_words(from_time, to_time, include_seconds)
end

此功能的行为与time_ago_in_words相同。如果from_time介于1 - 3周之前,则会打印last weektwo weeks agothree weeks ago,否则会打印常用内容。

答案 1 :(得分:0)

答案 2 :(得分:0)

此外,您可以为其他级别(日,月,年)编写自定义时间描述。

http://robots.thoughtbot.com/post/392707640/the-more-you-know-custom-time-descriptions

相关问题