ruby / rails新手,并为我的用户实施了邮箱消息传递。我希望在消息传递过程中显示用户个人资料头像,但似乎无法弄明白。头像在实际用户配置文件中显示正常。
我有头像显示,但此刻它只显示对话中两个用户的所有消息旁边的创始人头像。
据我所知,头像仅显示两个用户回复旁边的原始发件人头像,因为我正在使用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 :(得分:1)
问题是使用此方法conversation.originator.profile.avatar.url
在消息中显示对话发起人的图像。
因此,修复方法是使用消息,用户和个人资料之间的关联来获取发件人的图像。
message.sender.profile.avatar.url