2017-01-17 19 views
-1

我越來越想使用時,奇怪的錯誤‘time_ago_in_words’功能在軌conversation.messages.first(created_at):未定義的方法`> time_ago_in_words’Rails中

<% 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 %> 

未定義的方法`>'爲零:NilClass。

+6

'conversation.messages'必須爲空,因此'first'返回'nil' – Iceman

回答

1

Iceman砸在頭上。

在您的<li>請確保conversation.message存在,然後致電.first;否則.first將只返回nil

相關問題