简体   繁体   中英

Can you recommend a plugin or gem that generates a facebook-style message inbox using Rails 3?

I want a way for users to send and receive messages and to see them in an inbox similar to how it is done in Facebook: it shows the subject and knows whether the particular message was received or sent, and then clicking shows the entire thread.

I have been trying to use a single Message record with a UserMessage -- one for the sender, the other for the recipient -- but not exactly sure how to, for example, show all the messages for a User whether a recipient or sender.

Ideally, someone has already done this in a plugin or gem I can repurpose.

has_mailbox gem

is there to send messages between users, I'm testing it on my sample porject right now:) and dont forget to add will_paginate gem in gem file because for now ha_mailbox gem shows error incase will_paginate is not in gem file. I think it is dependent on it

This is not a very difficult task for you to handle. We want to be able to send messages between users. Let's say we have a User model that looks something like (for the sake of simplicity)

create_table "users", :force => true do |t|
    t.string   "username",        :null => false
    t.string   "hashed_password", :null => false
    t.datetime "created_at"
    t.datetime "updated_at"
    t.string   "salt",            :null => false
end

and a Message model that looks like (also for the sake of simplicity)

create_table "messages", :force => true do |t|
    t.string   "subject",       :null => false
    t.integer  "sender_id"
    t.integer  "recipient_id"
    t.datetime "created_at"
    t.datetime "updated_at"
end

and a Msgcontent model that looks like

 create_table "msgcontents", :force => true do |t|
    t.string   "body",       :null => false
    t.integer  "message_id"
    t.integer  "sender_id"
    t.datetime "created_at"
    t.datetime "updated_at"
end

Now that you have those models set up, in your controller you might have something like.

def show_all_messages
    @sent_messages = Message.find_all_by_sender_id(params[:user_id])
    @received_messages = Message.find_all_by_recipient_id(params[:user_id])
end

def show_message_detail
    @thread = Msgcontent.find_all_by_message_id(params[:message_id])
end

Where params[:user_id] is the current user signed in and params[:message_id] is the message that you want to show in detail. You can also combine the 2 variables into one variable, but you might want to separate it, it's up to you. This way, in your view you can appropriately display all the messages that have been sent and received by a user. I'm not going to write a whole view worth of code, but basically there's the show_all_messages that gets all messages that the user is a part of, and the show_message_detail will get the whole thread of the message. I will leave this part to you:)

If you want to be able to tell whether a user has seen the message or not, you can add 2 fields to your Message model, which would then result into

create_table "messages", :force => true do |t|
    t.string   "subject",              :null => false
    t.integer  "sender_id"
    t.integer  "recipient_id"
    t.datetime "sender_lastviewed",    :null => false
    t.datetime "recipient_lastviewed", :null => false
    t.datetime "created_at"
    t.datetime "updated_at"
end

Now you will know the last date that the sender or recipient has looked at your message. In your show_all_messages you would need to update your Message model to reflect the two columns you've added to your table. Then to able to get the messages after the _lastviewed you need to use conditions in your code, which can be found here

If you really want to find a gem, you can probably can with some diligent googling, however there are some very basic Rails (and database) concepts here that is essential for you to understand if you want to rely on your own knowledge for future development.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM