Ruby onRailsスコープ内の名前エラー

ルビーグプタ

プロジェクトに問題があります。チャットボックスを作成しようとしています。しかし、同じエラーが発生するたびに、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]

編集
0

コメントを追加

0

関連記事

分類Dev

Ruby onRails-BundlerMySQLエラー

分類Dev

Ruby onRails-配列内のデータベースクエリ

分類Dev

Ruby onRailsのメーラー

分類Dev

Ruby onRailsコントローラーからのPostgreSQLダンプ

分類Dev

Ruby OnRailsインストールでの構文エラー

分類Dev

Ruby OnRailsインストールでの構文エラー

分類Dev

Ruby onRailsの各ループ

分類Dev

Ruby onRailsコントローラーのテスト

分類Dev

Ruby onRailsアプリの致命的なエラー

分類Dev

Ruby onRailsのブートストラップ

分類Dev

Ruby onRailsの状態をブートストラップアコーディオンにラップする方法

分類Dev

Ruby onRailsでブートストラップを使用するエラー

分類Dev

Ruby onRails内の生のSQLクエリにパラメータを渡す

分類Dev

Ruby onRailsシードデータベースの構文エラー

分類Dev

Ruby onRailsエラーレーキdb:migrate

分類Dev

Ruby名エラー

分類Dev

Ruby onRails-グループ内のユーザー

分類Dev

Ruby onRailsモデル作成エラー

分類Dev

Ruby onRailsレポートのページ付けエラー

分類Dev

Ruby onRailsスキャフォールディングエラー

分類Dev

Ruby OnRailsサービスコンテナ

分類Dev

Ruby onRailsのシードファイルエラー

分類Dev

Ruby onRails-エラールートの正規表現

分類Dev

Ruby onRails-シード未定義のメソッドエラー

分類Dev

Ruby onRailsのコントローラーとモデル

分類Dev

Ruby onRailsブートストラップモーダル

分類Dev

Ruby onRailsのアクティブレコードクエリ

分類Dev

Ruby onRailsテスト-テーブルXにはYという名前の列がありません

分類Dev

Ruby onRailsの「初期化されていない定数」エラー

Related 関連記事

  1. 1

    Ruby onRails-BundlerMySQLエラー

  2. 2

    Ruby onRails-配列内のデータベースクエリ

  3. 3

    Ruby onRailsのメーラー

  4. 4

    Ruby onRailsコントローラーからのPostgreSQLダンプ

  5. 5

    Ruby OnRailsインストールでの構文エラー

  6. 6

    Ruby OnRailsインストールでの構文エラー

  7. 7

    Ruby onRailsの各ループ

  8. 8

    Ruby onRailsコントローラーのテスト

  9. 9

    Ruby onRailsアプリの致命的なエラー

  10. 10

    Ruby onRailsのブートストラップ

  11. 11

    Ruby onRailsの状態をブートストラップアコーディオンにラップする方法

  12. 12

    Ruby onRailsでブートストラップを使用するエラー

  13. 13

    Ruby onRails内の生のSQLクエリにパラメータを渡す

  14. 14

    Ruby onRailsシードデータベースの構文エラー

  15. 15

    Ruby onRailsエラーレーキdb:migrate

  16. 16

    Ruby名エラー

  17. 17

    Ruby onRails-グループ内のユーザー

  18. 18

    Ruby onRailsモデル作成エラー

  19. 19

    Ruby onRailsレポートのページ付けエラー

  20. 20

    Ruby onRailsスキャフォールディングエラー

  21. 21

    Ruby OnRailsサービスコンテナ

  22. 22

    Ruby onRailsのシードファイルエラー

  23. 23

    Ruby onRails-エラールートの正規表現

  24. 24

    Ruby onRails-シード未定義のメソッドエラー

  25. 25

    Ruby onRailsのコントローラーとモデル

  26. 26

    Ruby onRailsブートストラップモーダル

  27. 27

    Ruby onRailsのアクティブレコードクエリ

  28. 28

    Ruby onRailsテスト-テーブルXにはYという名前の列がありません

  29. 29

    Ruby onRailsの「初期化されていない定数」エラー

ホットタグ

アーカイブ