2017-01-14 20 views
1

ルビー/レールの新機能で、ユーザー向けにメールボックスメッセージングを実装しました。私は、メッセージ中にユーザープロファイルのアバターを表示したいが、それを理解しているようには見えない。アバターは、実際のユーザープロファイルで正常に表示されます。Mailboxerに送信者と受信者のアバターを表示するにはどうすればよいですか?

私はアバターを表示していますが、現時点では、会話の両方のユーザーのすべてのメッセージの隣に発信者のアバターが表示されます。

私はconversation.originatorを使用しているため、アバターは両方のユーザーの返信の隣に元の送信者のアバターを表示していることを理解します。現時点では、アバターを表示させることができるのは唯一の方法なので、これが私の出発点です。

送信者と受信者の両方のアバターを自分の返信/メッセージの隣に表示するにはどうすればよいですか?

また、私はRubyを初めて使っているので、これは最も簡単なことかもしれません。これが重複している場合はお詫びしますが、私は答えを見つけることができません。

_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> 

モデル/ 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 

モデル/ 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 
+0

'conversation.originator.profile'は、常に会話を開始したユーザーのプロファイルを提供するようです。 'model.rb'ファイルを表示できますか?' originator'メソッドは何をしていますか? – sahil

+0

私は上記の編集を行い、ユーザーモデルを追加しました。私は発信者メソッドがどこにでも定義されていない、私はそれがメールボックスの宝石であらかじめ定義されたものだと思ったからです。 – ChrisJC2017

+0

** conversation.originator.profile.avatar.url **をこの** message.sender.user.profile.avatar.url **に置き換えて確認できますか?送信者がモデルであり、ユーザーと送信者の間に関連があるか、送信者がユーザーかどうかはわかりません。 – sahil

答えて

1

は、問題は、この方法conversation.originator.profile.avatar.urlを使用して、部分的メッセージでの会話の発信者の画像を見せていました。

したがって、メッセージ、ユーザー、およびプロファイルの間の関連付けを使用して、送信者のイメージを取得します。

message.sender.profile.avatar.url 
関連する問題