How do you do relative time in Rails?

前端 未结 11 1580
借酒劲吻你
借酒劲吻你 2020-12-02 03:43

I\'m writing a Rails application, but can\'t seem to find how to do relative time, i.e. if given a certain Time class, it can calculate \"30 seconds ago\" or \"2 days ago\"

11条回答
  •  爱一瞬间的悲伤
    2020-12-02 04:27

    Since the most answer here suggests time_ago_in_words.

    Instead of using :

    <%= time_ago_in_words(comment.created_at) %>
    

    In Rails, prefer:

    
      <%= comment.created_at.to_s %>
    
    

    along with a jQuery library http://timeago.yarp.com/, with code:

    $("abbr.timeago").timeago();
    

    Main advantage: caching

    http://rails-bestpractices.com/posts/2012/02/10/not-use-time_ago_in_words/

提交回复
热议问题