如何在Mailboxer中显示发送者和接收者的头像?
How to display sender and receiver avatars in Mailboxer?
ruby/rails 的新手,刚刚为我的用户实现了邮箱消息传递。我希望在消息传递期间显示用户个人资料头像,但似乎无法弄清楚。头像在实际用户个人资料上显示良好。
我显示了头像,但目前它只在对话中两个用户的所有消息旁边显示发起人头像。
我知道头像只显示两个用户回复旁边的原始发件人头像,因为我使用的是 conversation.originator
。目前,这是我什至可以让头像出现的唯一方法,所以这是我的起点。
如何让发送者和接收者的头像都显示在他们自己的旁边 replies/messages?
同样,我是 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>
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
问题是使用此方法 conversation.originator.profile.avatar.url
.
在部分消息中显示对话发起者的图像
因此解决方法是使用消息、用户和配置文件之间的关联来获取发件人的图像。
message.sender.profile.avatar.url
ruby/rails 的新手,刚刚为我的用户实现了邮箱消息传递。我希望在消息传递期间显示用户个人资料头像,但似乎无法弄清楚。头像在实际用户个人资料上显示良好。
我显示了头像,但目前它只在对话中两个用户的所有消息旁边显示发起人头像。
我知道头像只显示两个用户回复旁边的原始发件人头像,因为我使用的是 conversation.originator
。目前,这是我什至可以让头像出现的唯一方法,所以这是我的起点。
如何让发送者和接收者的头像都显示在他们自己的旁边 replies/messages?
同样,我是 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>
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
问题是使用此方法 conversation.originator.profile.avatar.url
.
因此解决方法是使用消息、用户和配置文件之间的关联来获取发件人的图像。
message.sender.profile.avatar.url