ruby-on-railsrubymailboxer

How to display sender and receiver avatars in Mailboxer?


New to ruby/rails, and just implemented mailboxer messaging for my users. 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 . 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.

_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

<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

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

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

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

Solution

  • The problem was showing the conversation originator's image in the messages partial by using this method 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