【问题标题】:Rails Mailboxer/messaging -- Only return non-empty arraysRails Mailboxer/messaging -- 只返回非空数组
【发布时间】:2012-11-12 15:57:15
【问题描述】:

我正在使用mailboxer gem 来构建一个私人消息系统。

我想遍历我的用户列表,并在每个用户名旁边提供一个链接以打开一个预先存在的对话(如果存在),或者如果不存在对话则创建一个新对话。 (因此用户只能与另一个用户进行 1 次对话,而不是像系统一样的电子邮件)。

<% @users.each do |u| %>
  <%= link_to u.name, u %>
  <% @mailbox.conversations.each do |c| %>
    <% if c.receipts.where(:receiver_id => user).present? %>
     true
    <% else %>
     false
    <% end %>
  <% end %>
<% end %>

所以“true”将是重新打开对话的链接,而“false”将是新对话的按钮。

但如果一个用户有 4 个对话和 1 个与给定用户的对话,它将返回 true false false false。所以 1 继续按钮和 3 创建新按钮。所以本质上,我需要整个事情返回真或假

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 loops private-messaging


    【解决方案1】:

    如果有人感兴趣,这是一个更优雅的解决方案。只是在控制器中。那么所有的链接都是一样的,如果存在就会打开消息。 Conversation.participant(a).participant(b) 过滤是关键

    def new
      @recipient = User.find(params[:recipient])
      @ongoing_conversation = Conversation.participant(current_user).participant(@recipient)
    
      respond_to do |format|
        format.html { redirect_to root_path }
        if @ongoing_conversation.present?
          format.js { redirect_to mailbox.conversations.find(@ongoing_conversation) }
        else
          format.js
        end
      end
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-03
      • 1970-01-01
      • 2018-06-19
      • 2020-09-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多