[英]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.我想要一种让用户发送和接收消息并在收件箱中查看它们的方法,类似于在 Facebook 中的操作方式:它显示主题并知道是否接收或发送了特定消息,然后单击显示整个线程。
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.我一直在尝试将单个消息记录与 UserMessage 一起使用——一个用于发件人,另一个用于收件人——但不完全确定如何显示用户的所有消息,例如,无论是收件人还是发件人。
Ideally, someone has already done this in a plugin or gem I can repurpose.理想情况下,有人已经在我可以重新利用的插件或 gem 中做到了这一点。
has_mailbox
gem has_mailbox
宝石
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.是否可以在用户之间发送消息,我现在正在我的示例项目上对其进行测试:) 并且不要忘记在 gem 文件中添加 will_paginate gem,因为现在 ha_mailbox gem 显示错误,以防 will_paginate 不在 gem 文件中。 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)假设我们有一个
User
model 看起来像(为简单起见)
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)和一条
Message
model 看起来像(也是为了简单起见)
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和一个看起来像的
Msgcontent
model
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.现在您已经设置了这些模型,在您的 controller 中您可能会有类似的东西。
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.其中
params[:user_id]
是当前登录用户, params[:message_id]
是您要详细显示的消息。 You can also combine the 2 variables into one variable, but you might want to separate it, it's up to you.您也可以将 2 个变量合并为一个变量,但您可能需要将其分开,这取决于您。 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.我不会编写完整的代码视图,但基本上有
show_all_messages
获取用户所属的所有消息,而show_message_detail
将获取消息的整个线程。 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如果您希望能够判断用户是否已看到该消息,您可以在
Message
model 中添加 2 个字段,这将导致
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.在您的
show_all_messages
中,您需要更新您的Message
model 以反映您添加到表中的两列。 Then to able to get the messages after the _lastviewed
you need to use conditions in your code, which can be found here然后为了能够在
_lastviewed
之后获取消息,您需要在代码中使用条件,可以在此处找到
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.如果你真的想找到一个 gem,你可能可以通过一些勤奋的谷歌搜索,但是这里有一些非常基本的 Rails(和数据库)概念,如果你想依靠自己的知识进行未来的开发,这些概念对于你来说是必不可少的.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.