问题
I am using the mailboxer gem to send messages within my ruby on rails app. The gem is working. I can't for the life of me figure out how to access the recipient on the sent messages page. On the sent messages page, I am trying to loop through sent messages, and access the recipient of the sent messages and display that person to the user.
On the regular inbox page, I used the following:
<% all_mail = current_user.mailbox.inbox %>
<% all_mail.each do |letter| %>
<tr>
<td><%= letter.messages.last.sender.email %></td>
<td><%= letter.messages.last.subject %></td>
</tr>
<% end %>
I am trying to make a similar look for the sent messages page, but I can't figure out how to do it(specifically access the email of the recipient).
I can't use:
#user wants to retrieve all his conversations
user.mailbox.conversations
#user wants to retrieve his inbox
user.mailbox.inbox
#user wants to retrieve his sent conversations
user.mailbox.sentbox
because there is no way to organize how the messages are displayed using those methods (at least as far as I know). How can I loop through the messages and find the message recipient for each message?
回答1:
You have the participants in conversations (you call it letters).
Just filter the current user and you'll be good.
conversation = user.mailbox.sentbox.first
filtered_participants = conversation.participants.reject do |u|
u.id == current_user.id
end
回答2:
Each message has recipients, which is a list. Assuming that your conversations are just between two users you can grab for the first list's element:
<% conversation.messages.each do |message| %>
<%= message.recipients.first.email %>, <%= message.body %>
<% end %>
来源:https://stackoverflow.com/questions/21417883/how-to-access-recipient-on-sent-messages-page-with-mailboxer