Rails - how to display time created as twitter

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

Note that the time is displayed with the following logic:

  • If created_at , before 24 hours, it is displayed XXX hours ago, or 1 hour ago
  • If created_at , more than 24 hours, X Feb is displayed

Does Rails have this feature built in, or does it need a helper? If so, what smart way to decide for this type of output?

thanks

+7
source share
2 answers

You are probably looking for the helper method time_ago_in_words .

+6
source

I know this is a little late, but this is what I use in my view to display my posts:

 <% 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 retrieved in a helper method. hope this helps

+5
source

All Articles