I get this error when I try to use time_ago_in_words
:
Comparison of String with ActiveSupport::Duration failed
I'm trying to check whether an object was created more than 8 minutes ago:
<% if 开发者_开发百科time_ago_in_words(obj.created_at) > 8.minutes %>
<p>Yes</p>
<% end %>
Would appreciate it if anyone knows the correct way to perform this test.
time_ago_in_words
returns a phrase meant to be used in your UI. If you're comparing dates with each other, you're going to want to do it before it's translated into a user-friendly string.
Also note that I used minutes.ago
in order to compare apples with apples.
<% if obj.created_at > 8.minutes.ago %>
Within the last 8 minutes
<% else %>
Longer than 8 minutes ago
<% end %>
精彩评论