プロジェクトに問題があります。チャットボックスを作成しようとしています。しかし、同じエラーが発生するたびに、conversation.rbの未定義のローカル変数またはメソッド `user 'が表示されます。私は間違っていたのか分かりません。
私のウェブページで受け取るエラーは次のとおりです。
NameError in ConversationsController#show
undefined local variable or method `user' for #<Module:0x7351368>
Rails.root: C:/Users/Arvind/project/book
Application Trace | Framework Trace | Full Trace
app/models/conversation.rb:10:in `block in <class:Conversation>'
app/models/conversation.rb:9:in `<class:Conversation>'
app/models/conversation.rb:1:in `<top (required)>'
app/controllers/conversations_controller.rb:17:in `show'
私の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
これは私の会話コントローラーです。rbは次のようになります。
class ConversationsController < ApplicationController
before_filter :authenticate_user!
layout false
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
render json: { conversation_id: @conversation.id }
end
def show
@conversations = Conversation.involving(User.first)
@reciever = interlocutor(@conversation)
@messages = @conversation.messages
@message = Message.new
end
private
def conversation_params
params.permit(:sender_id, :recipient_id)
end
def interlocutor(conversation)
current_user == conversation.recipient ? conversation.sender : conversation.recipient
end
終わり
ありがとうございました!!
以下のコードを試してください
scope :involving, ->(user) { where("conversations.sender_id =? OR conversations.recipient_id =?", user.id, user.id) }
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加