【问题标题】:Show username on Mailboxer inbox in Rails在 Rails 的 Mailboxer 收件箱上显示用户名
【发布时间】:2014-01-07 10:23:41
【问题描述】:

我在我的 Rails 应用程序中使用 Mailboxer (https://github.com/ging/mailboxer) gem 来处理用户之间的私人消息。根据文档,我使用= render mailbox.inbox 显示登录用户的所有对话(消息)的列表。但是,该方法仅输出消息的主题。我需要显示邮件的发件人以及主题。如何编辑此输出以显示发件人姓名?

SQL 表显示发件人姓名未存储在会话表中,而是通过通知表中的通知(一对多)存储在通知表中。我尝试使用<%= 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

【问题讨论】:

    标签: ruby-on-rails ruby private-messaging mailboxer


    【解决方案1】:

    sender_id 可使用receipt.notification.sender_id 和.sender_type 获得

    对于任何对话,您都可以使用以下方法检索收据: @receipts = conversation.receipts_for

    【讨论】:

      【解决方案2】:

      我在某个时候遇到过同样的问题,我的解决方案是这样的:accessing current_user in mailboxer mailer notification

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-01-02
        • 2018-03-31
        • 2015-05-11
        • 2011-08-12
        相关资源
        最近更新 更多