0

ユーザー間のプライベート メッセージを処理するために、Rails アプリでMailboxer ( https://github.com/ging/mailboxer ) gem を使用しています。ドキュメントに従って、= render mailbox.inboxログインしているユーザーのすべての会話 (メッセージ) のリストを表示するために使用しています。ただし、このメソッドはメッセージの件名のみを出力します。メッセージの送信者と件名を表示する必要があります。送信者名が表示されるようにこの出力を編集するにはどうすればよいですか?

SQL テーブルは、送信者名が会話テーブルに保存されていないことを示していますが、notifications_on_conversation_id (1 対多) を介して通知テーブルに保存されています。使用しようとしまし<%= conversation.notifications.sender_id %>たが、次のエラーが発生しました。

NoMethodError in Conversations#index
undefined method `notifications' for nil:NilClass

会話のユーザーと件名を表示するために、会話コントローラーとビューに何を追加できますか?

ありがとう!

会話コントローラー:

class ConversationsController < ApplicationController
  before_filter :authenticate_user!
  helper_method :mailbox, :conversation

  def create
    recipient_emails = conversation_params(:recipients).split(',')
    recipients = User.where(email: recipient_emails).all

    conversation = current_user.
      send_message(recipients, *conversation_params(:body, :subject)).conversation

    redirect_to conversation
  end

  def reply
    current_user.reply_to_conversation(conversation, *message_params(:body, :subject))
    redirect_to conversation
  end

  def trash
    conversation.move_to_trash(current_user)
    redirect_to :conversations
  end

  def untrash
    conversation.untrash(current_user)
    redirect_to :conversations
  end

  private

  def mailbox
    @mailbox ||= current_user.mailbox
  end

  def conversation
    @conversation ||= mailbox.conversations.find(params[:id])
  end

  def conversation_params(*keys)
    fetch_params(:conversation, *keys)
  end

  def message_params(*keys)
    fetch_params(:message, *keys)
  end

  def fetch_params(key, *subkeys)
    params[key].instance_eval do
      case subkeys.size
      when 0 then self
      when 1 then self[subkeys.first]
      else subkeys.map{|k| self[k] }
      end
    end
  end
end
4

2 に答える 2