订阅会话中的消息 - rails

时间:2014-03-14 22:46:18

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

我使用名为mailboxer的gem来允许用户在彼此之间发送消息:

以下是我在对话控制器中的内容:

class ConversationsController < ApplicationController
  ...

  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

这就是我在对话中所看到的观点:

<p> <%= conversation.subject %> </p>


 <%= content_tag_for(:p, conversation.receipts_for(current_user)) do |receipt| %>
    <% message = receipt.message %>


    <p>
      <%=  message.body %>
    </p>

  <% end %>


<%= render 'messages/form', conversation: conversation %>

哪个有效,但这些消息都是乱码的。 如何在创建对话时订购对话的消息?

我的架构与邮箱有关:

  create_table "receipts", force: true do |t|
    t.integer  "receiver_id"
    t.string   "receiver_type"
    t.integer  "notification_id",                            null: false
    t.boolean  "is_read",                    default: false
    t.boolean  "trashed",                    default: false
    t.boolean  "deleted",                    default: false
    t.string   "mailbox_type",    limit: 25
    t.datetime "created_at",                                 null: false
    t.datetime "updated_at",                                 null: false
  end

  add_index "receipts", ["notification_id"], name: "index_receipts_on_notification_id"


  create_table "conversations", force: true do |t|
    t.string   "subject",    default: ""
    t.datetime "created_at",              null: false
    t.datetime "updated_at",              null: false
  end

  create_table "notifications", force: true do |t|
    t.string   "type"
    t.text     "body"
    t.string   "subject",              default: ""
    t.integer  "sender_id"
    t.string   "sender_type"
    t.integer  "conversation_id"
    t.boolean  "draft",                default: false
    t.datetime "updated_at",                           null: false
    t.datetime "created_at",                           null: false
    t.integer  "notified_object_id"
    t.string   "notified_object_type"
    t.string   "notification_code"
    t.string   "attachment"
    t.boolean  "global",               default: false
    t.datetime "expires"
  end

  add_index "notifications", ["conversation_id"], name: "index_notifications_on_conversation_id"

1 个答案:

答案 0 :(得分:0)

我认为您只需要订购这样的收据:

 <%= content_tag_for(:p, conversation.receipts_for(current_user).order(:created_at)) do |receipt| %>
相关问题