简体   繁体   English

如何在 Mailboxer 中显示发件人和收件人头像?

[英]How to display sender and receiver avatars in Mailboxer?

New to ruby/rails, and just implemented mailboxer messaging for my users. ruby/rails 新手,刚刚为我的用户实现了邮箱消息传递。 I want the users profile avatar to display during messaging, but can't seem to figure it out.我希望在消息传递过程中显示用户个人资料头像,但似乎无法弄清楚。 Avatars display fine on the actual user profiles.头像在实际用户配置文件中显示良好。

I have the avatar displaying, but at the moment it just shows the originators avatar next to all messages of both users in the conversation.我有头像显示,但目前它只在对话中两个用户的所有消息旁边显示发起者头像。

I understand that the avatar is only displaying the original senders avatar next to both users replies, because I'm using conversation.originator .我知道该头像仅在两个用户回复旁边显示原始发件人头像,因为我使用的是conversation.originator At the moment, it's the only way I can even get the avatar to appear, so that is my starting point.目前,这是我什至可以让头像出现的唯一方法,所以这是我的起点。

How do I get both sender, and receivers avatars to display next to their own replies/messages?如何让发件人和收件人的头像显示在他们自己的回复/消息旁边?

Again, I'm new to Ruby, so this may be the simplest thing, and apologies if this is a duplicate, but I can't seem to find an answer.同样,我是 Ruby 的新手,所以这可能是最简单的事情,如果这是重复的,我很抱歉,但我似乎找不到答案。

_messages.html.erb _messages.html.erb

<% @receipts.each do |receipt| %>
    <% message = receipt.message %>
    <div class="media">
      <div class="media-left">
        <%= image_tag conversation.originator.profile.avatar.url, class: 'user-show-avatar' %>

      </div>
      <div class="media-body">
        <h4 class="media-heading">
          <%= message.sender.name %> <br>
          <small><b>Subject: </b><%= message.subject %></small><br>
          <small><b>Date: </b><%=  message.created_at.strftime("%A, %b %d, %Y at %I:%M%p") %></small>
        </h4>
        <%= message.body %>
      </div>
    </div>
<% end %>

_conversation.html.erb _conversation.html.erb

<div class="media">
  <div class="media-left">

    <%= image_tag conversation.originator.profile.avatar.url, class: 'user-show-avatar' %>
  </div>
  <div class="media-body">
    <h4 class="media-heading">
      <%= conversation.originator.name %> <br>
      <small><b>Subject: </b><%= conversation.subject %></small><br>
      <small><b>Date: </b><%=  conversation.messages.last.created_at.strftime("%A, %b %d, %Y at %I:%M%p") %></small>
    </h4>
    <%= truncate conversation.messages.last.body, length: 145 %>
    <%= link_to "View", conversation_path(conversation)  %>
    <% if conversation.is_trashed?(current_user) %>
        <%= link_to 'Untrash', untrash_conversation_path(conversation), method: :post %>
    <% else %>
        <%= link_to 'Trash', trash_conversation_path(conversation), method: :post,
                    data: {confirm: 'Are you sure?'} %>
    <% end %>
  </div>
</div>

models/profile.rb模型/配置文件.rb

class Profile < ActiveRecord::Base
  belongs_to :user

  has_attached_file :avatar, default_url: "/images/:style/missing.png"
  validates_attachment_content_type :avatar, content_type: /\Aimage\/.*\Z/

end

conversations_controller.rb对话_控制器.rb

class ConversationsController < ApplicationController
  before_action :authenticate_user!

 def new
    @user = User.find_by(id: params[:recipient_id])
  end

   def create
    recipients = User.find_by(id: params[:recipient_id])
    conversation = current_user.send_message(recipients, conversation_params[:body], conversation_params[:subject]).conversation
    flash[:success] = "Your message was successfully sent!"
    redirect_to conversation_path(conversation)
  end

  def show
    @receipts = conversation.receipts_for(current_user).order("created_at ASC")
    # mark conversation as read
    conversation.mark_as_read(current_user)
  end


   def reply
    current_user.reply_to_conversation(conversation, message_params[:body])
    flash[:notice] = "Your reply message was successfully sent!"
    redirect_to conversation_path(conversation)
  end

   def trash
    conversation.move_to_trash(current_user)
    redirect_to mailbox_inbox_path
  end

  def untrash
    conversation.untrash(current_user)
    redirect_to mailbox_inbox_path
  end

  private

  def conversation_params
    params.require(:conversation).permit(:subject, :body, recipients:[])
  end

  def message_params
    params.require(:message).permit(:body, :subject)
  end
end

models/user.rb模型/用户.rb

class User < ApplicationRecord
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  has_one :profile
  belongs_to :plan
  acts_as_messageable

def mailboxer_name
self.name
end

def mailboxer_email(object)
self.email
end

end

The problem was showing the conversation originator's image in the messages partial by using this method conversation.originator.profile.avatar.url .问题是通过使用这种方法conversation.originator.profile.avatar.url在消息部分中显示对话发起者的图像。

So the fix is to use the association between messages, user and profile to get the image of the sender.所以解决方法是使用消息、用户和配置文件之间的关联来获取发件人的图像。

message.sender.profile.avatar.url

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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