0
votes

I am receiving undefined method on `message_time' for nil:NilClass:

Showing /home/ubuntu/workspace/app/views/conversations/index.html.erb where line #19 raised:

undefined method `message_time' for nil:NilClass Extracted source (around line #19): 17 18 19 20 21 22

                            <div class="col-md-2">
                                <%= other.fullname %><br>
        19    <%= conversation.messages.last.message_time %>
                            </div>
                            <div class="col-md-8">
                                <%= conversation.messages.last.content %>

app/views/conversations/index.html.erb

<div class="row">
<div class="col-md-12">

    <div class="panel panel-default">
        <div class="panel-heading">Your conversations</div>
        <div class="panel-body">
            <div class="container">
                <% @conversations.each do |conversation| %>
                    <% other = conversation.sender == current_user ? conversation.recipient : conversation.sender %>

                    <%= link_to conversation_messages_path(conversation) do %>

                        <div class="row conversation">
                            <div class="col-md-2">
                                <%= image_tag avatar_url(other), class: "img-circle avatar-medium" %>
                            </div>
                            <div class="col-md-2">
                                <%= other.fullname %><br>
                                <%= conversation.messages.last.message_time %>
                            </div>
                            <div class="col-md-8">
                                <%= conversation.messages.last.content %>
                            </div>
                        </div>

                    <% end %>
                <% end %>
            </div>
        </div>
    </div>

    <h3>All Users</h3>
    <% @users.each do |user| %>
        <% if user != current_user %>
            <%= user.fullname %>
            <%= link_to "Send Message", conversations_path(sender_id: current_user.id, recipient_id: user.id), method: 'post' %>
        <% end %>
        <br>
    <% end %>

</div>

Conversation controller

class ConversationsController < ApplicationController
before_action :authenticate_user!

def index
    @users = User.all
    @conversations = Conversation.involving(current_user)
end

def create
    if Conversation.between(params[:sender_id], params[:recipient_id]).present?
        @conversation = Conversation.between(params[:sender_id], params[:recipient_id]).first
    else
        @conversation = Conversation.create(conversation_params)
    end

    redirect_to conversation_messages_path(@conversation)
end

private

    def conversation_params
        params.permit(:sender_id, :recipient_id)
    end

end

Conversation.rb

class Conversation < ActiveRecord::Base
    belongs_to :sender, foreign_key: :sender_id, class_name: 'User'
    belongs_to :recipient, foreign_key: :recipient_id, class_name: 'User'

    has_many :messages, dependent: :destroy

    validates_uniqueness_of :sender_id, scope: :recipient_id
    scope :involving, -> (user) do
        where("conversations.sender_id = ? OR conversations.recipient_id = ?", user.id, user.id)
    end

    scope :between, -> (sender_id, recipient_id) do 
        where("(conversations.sender_id = ? AND conversations.recipient_id = ?) OR (conversations.sender_id = ? AND conversations.recipient_id = ?)", 
                sender_id, recipient_id, recipient_id, sender_id)
    end

end
2

2 Answers

1
votes

I think conversation has no messages and so conversation.messages.last is nil.

You are calling message_time on a nil object and that's what the error message says. Before calling message_time, make sure converation.messages is not empty. You could solve this by

<% messages = conversation.messages %>
<%= messages.last.message_time unless messages.empty? %>
0
votes

Other option is

<% messages = conversation.messages %>
<%= messages.last.try(:message_time) %>