【问题标题】:Mailboxer + Rails 4.1: Message to myself shows duplicate sentbox and inbox message in conversationMailboxer + Rails 4.1:给自己的消息在对话中显示重复的发件箱和收件箱消息
【发布时间】:2014-08-19 18:49:34
【问题描述】:

将mailboxer gem 0.12.2 版与Rails 4.1.4 版一起使用。

当我向自己发送消息时,“对话”视图会显示相同的消息两次(一次是 mailbox_type=sentbox,一次是 mailbox_type=inbox)。我对发送给其他用户的消息没有这个问题。

我只想在我的对话视图中显示一次消息(理想情况下,查看我的收件箱时是 inbox 版本,查看我发送的消息时是 sentbox 版本。)有什么办法可以做到吗?

这是我的自定义ConversationsController 中的show 操作(current_user 是一种指示当前登录用户的设计方法)

def show
  @mailbox = current_user.mailbox
  @conversation = @mailbox.conversations.find(params[:id])
  @receipts = @conversation.receipts_for(current_user)
end

我还在我的show 操作中尝试了以下组合,结果都相同。

  • 使用inbox 方法代替conversations 方法:
    @conversation = @mailbox.inbox.find(params[:id])
  • @mailbox 实例变量上使用receipts_for 方法:
    @receipts = @mailbox.receipts_for(@conversation)

这是我对应的show.html.erb 视图

<ul>
  <%= content_tag_for(:li, @receipts) do |receipt| %>
    <% message = receipt.message %>
    <strong>From:</strong> <%= message.sender.email %>
    <br/>
    <strong>Message:</strong><%= simple_format h message.body %>
    <strong>Sent:</strong> <%= @conversation.updated_at.strftime("%a, %m/%e/%Y %I:%M %p") %>

    <div>
      <br/>DEBUG INFO:<br/>
      Message Id <%= message.id %><br/>
      Receipt Id <%= receipt.id %><br/>
      Receipt Mailbox type <%= receipt.mailbox_type %><br/>
    </div>
    <hr/>

  <% end %>
</ul>

有没有办法让我的“收件箱”视图用于与我自己的对话而不显示重复的消息?

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-4 mailboxer


    【解决方案1】:

    感谢 searsaw 在gem's github page 上回答这个问题:

    def show
      @mailbox = current_user.mailbox
      @conversation = Mailboxer::Conversation.find(params[:id])
      @receipts = @conversation.receipts_for(current_user).inbox
    end
    

    执行此操作的行是@conversation.receipts_for(current_user).inbox。在receiptsgets the receipts for a logged in user from that conversation that are labeled with the mailbox_type of inbox.”上调用inbox

    限制

    不幸的是,如果您在receipts 上使用inbox 方法(current_user 发送的消息),则对话的show 视图的@receipts 变量的内容可能不适用于与其他用户的对话在对话的show 视图中与其他用户不可见。这是mailboxer gem 的一个限制(Mailboxer 的设计目的不是只能向您自己发送消息)。

    最后,如果您需要支持仅向自己发送消息,则需要控制器/视图中的额外逻辑。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-02-01
      • 2014-01-07
      • 1970-01-01
      • 2013-02-10
      • 2012-02-23
      • 1970-01-01
      • 2016-05-22
      • 1970-01-01
      相关资源
      最近更新 更多