conversation

mailboxer
1.在gemfile中添加

1
gem 'mailboxer', github: 'mailboxer/mailboxer'

$ bundle install
$ rails g mailboxer:install
$ rake db:migrate
$ rails g mailboxer:views(发邮件/notification时候的设置,对于message没有页面设置)

2.在model_user中添加

user.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  acts_as_messageable
def name
"User #{id}"
end
def mailboxer_email(object)
nil
end

(
#Returning any kind of identification you want for the model
def name
return "You should add method :name in your Messageable model"
end
#Returning the email address of the model if an email should be sent for this object (Message or Notification).
#If no mail has to be sent, return nil.
def mailboxer_email(object)
#Check if an email should be sent for that object
#if true
return "define_email@on_your.model"
#if false
#return nil
end
)

ps:在rails c上面测试是否安装成功

1
2
u = User.first
u.send_message User.last, "body", "subject"

3.

routes.rb
1
2
3
resources :conversations do
resources :messages
end

4.rails g controller conversations

conversations_controller.rb
1
2
3
def index
@conversations = current_user.mailbox.conversations #(sentbox+inbox+trash)
end
  1. touch app/views/conversations/index.html.erb
    index.html.erb
    1
    2
    3
    4
    <h1>所有对话</h1>
    <% @conversations.each do |conversation| %>
    <%= link_to conversation.subject %>
    <% end %>

ps:打开测试