开发者

Rails - How to display the time created like twitter

开发者 https://www.devze.com 2023-02-09 12:15 出处:网络
please take a look here: http://twitter.com/#!/techcrunch Notice that time is displayed with the following logic:

please take a look here: http://twitter.com/#!/techcrunch

Notice that time is displayed with the following logic:

  • If created_at, is under 24hrs, it display XXX hours ago, or 1 hour ago
  • If created_at, is over 24hrs, it display the X Feb

Does Rails have this capability built in or is a helper needed? If so, what's the smart way to solve for this type of ou开发者_如何学编程tput?

Thanks


You are probably looking for the time_ago_in_words helper method.


i know this is a bit late but this is what i use in my view to display my messages:

<% if message.created_at > Time.now.beginning_of_day %>
    <%="#{time_ago_in_words(message.created_at)} ago"%> # 3 minutes ago, less than a minute ago  
<% else %>
    <%= message.created_at.strftime("%b %d, %Y") %> # Jun 29, 2012 
<% end %>

Can be extracted into a helper method. hope it helps

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号