rails nil:NilClass ruby 的未定义方法“message_time”
undefined method `message_time' for nil:NilClass ruby on rails
我在 `message_time' 上收到未定义的方法 nil:NilClass:
显示 /home/ubuntu/workspace/app/views/conversations/index.html.erb 第 19 行出现的位置:
nil:NilClass 的未定义方法“message_time”
提取的源代码(大约第 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>
对话控制器
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
结束
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
我认为 conversation
没有 messages
所以 conversation.messages.last
是 nil
。
您正在 nil
对象上调用 message_time
,这就是错误消息所说的内容。在调用 message_time
之前,确保 converation.messages
不为空。你可以通过
解决这个问题
<% messages = conversation.messages %>
<%= messages.last.message_time unless messages.empty? %>
其他选项是
<% messages = conversation.messages %>
<%= messages.last.try(:message_time) %>
我在 `message_time' 上收到未定义的方法 nil:NilClass:
显示 /home/ubuntu/workspace/app/views/conversations/index.html.erb 第 19 行出现的位置:
nil:NilClass 的未定义方法“message_time” 提取的源代码(大约第 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>
对话控制器
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
结束
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
我认为 conversation
没有 messages
所以 conversation.messages.last
是 nil
。
您正在 nil
对象上调用 message_time
,这就是错误消息所说的内容。在调用 message_time
之前,确保 converation.messages
不为空。你可以通过
<% messages = conversation.messages %>
<%= messages.last.message_time unless messages.empty? %>
其他选项是
<% messages = conversation.messages %>
<%= messages.last.try(:message_time) %>