I'm using a gem called mailboxer to allow users to send messages among each other:
Here is what I have in my conversations controller:
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
And this is what I have in my conversations show view:
<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 %>
Which works, but the messages are all scrambled. How do I order the messages of the conversation by when they were created?
My schema pertaining to mailboxer:
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"