0

当我尝试使用时出现此错误time_ago_in_words

Comparison of String with ActiveSupport::Duration failed

我正在尝试检查是否在 8 分钟前创建了一个对象:

  <% if time_ago_in_words(obj.created_at) > 8.minutes  %>
    <p>Yes</p>
  <% end %>

如果有人知道执行此测试的正确方法,将不胜感激。

4

1 回答 1

3

time_ago_in_words返回要在您的 UI 中使用的短语。如果您要相互比较日期,您会希望在将其转换为用户友好的字符串之前执行此操作。

另请注意,我用来minutes.ago比较苹果和苹果。

<% if obj.created_at > 8.minutes.ago  %>
    Within the last 8 minutes
<% else %>
    Longer than 8 minutes ago
<% end %>
于 2009-10-30T03:25:29.807 回答