未定义的方法`>'对于nil:NilClass使用&time; time_ago_in_words'在Rails中

时间:2017-01-17 16:03:51

标签: html ruby-on-rails ruby

我在尝试使用' time_ago_in_words'时遇到了一个奇怪的错误。在rails中的conversation.messages.first(created_at)上运行:

<% conversations.each do |conversation| %>
  <% user_status = current_user.id == conversation.sender.id ? conversation.recipient : conversation.sender %>
  <li>
    <%= gravatar_for(user_status, size: 50) %>
    <%= link_to user_status.email, conversation_messages_path(conversation),
        remote: true,
        class: "conversation-names #{'current-link' if conversation == conversations.first}" %>
    <span class="delete-message"><%= link_to do %><i class="fa fa-times"></i><% end %></span>
    <p><%= truncate(conversation.messages.first.try(:body)) %><%= time_ago_in_words conversation.messages.first.try(:created_at) %></p>
  </li>
<% end %>

未定义的方法`&gt;&#39;为零:NilClass。

1 个答案:

答案 0 :(得分:1)

Iceman击中了头部。

<li> conversation.message中确保.first存在之前,先致电.first;否则nil将返回SELECT COUNT(DISTINCT pets.id),locations.attr -> 'country' as country FROM pets,photos,locations WHERE photos.pet_id = pets.id AND photos.location_id = locations.id GROUP BY pets.id,locations.attr -> 'country';