nil的未定义方法ID:NilClass

时间:2017-11-10 17:10:24

标签: ruby-on-rails methods null

我在RoR中收到此错误:

  

在2017-11-10 15:03:09 -0200开始发布“/ conversations?person_id = 3”for 127.0.0.1   由ConversationsController处理#create为JS     参数:{“person_id”=>“3”}   在1ms内完成500内部服务器错误(ActiveRecord:0.0ms)

     

NoMethodError(nil的未定义方法`id':NilClass):

     

app / controllers / conversations_controller.rb:3:在'create'

我的conversation_controller:

class Conversation < ApplicationRecord
  has_many :messages, dependent: :destroy
  belongs_to :sender, foreign_key: :sender_id, class_name: Person
  belongs_to :recipient, foreign_key: :recipient_id, class_name: Person

  validates :sender_id, uniqueness: { scope: :recipient_id }

  scope :between, -> (sender_id, recipient_id) do
  where(sender_id: sender_id, recipient_id: recipient_id).or(
    where(sender_id: recipient_id, recipient_id: sender_id)
  )
end

def self.get(sender_id, recipient_id)
  conversation = between(sender_id, recipient_id).first
  return conversation if conversation.present?

  create(sender_id: sender_id, recipient_id: recipient_id)
end

def opposed_person(person)
  person == recipient ? sender : recipient
end
end

会话模型:

var conversations = $('#conversations-list');
var conversation = conversations.find("[data-conversation-id='" + "<%=     @conversation.id %>" + "']");

if (conversation.length !== 1) {
   conversations.append("<%= j(render 'conversations/conversation',    conversation: @conversation, person: current_person) %>");
   conversation = conversations.find("[data-conversation-id='" + "<%=      @conversation.id %>" + "']");
 }

conversation.find('.panel-body').show();

var messages_list = conversation.find('.messages-list');
var height = messages_list[0].scrollHeight;
messages_list.scrollTop(height);

对话create.js

=MATCH(C7,OFFSET(rank_lookup!$A$3:$A$73,0,MATCH(D7,rank_lookup!$B$2:$E$2,0)),1)

1 个答案:

答案 0 :(得分:1)

以下似乎正在返回nil

 @conversation = Conversation.get(current_person.id, params[:person_id])

如果您无法使用断点进行调试,则可以执行以下操作:

puts "#{current_person.id} | #{params[:person_id]} | #{@conversation.to_sql}" 

看看有什么不对。

我想current_person在其他地方设置,就像设计中的current_user一样。