通过Actioncable使用群聊查找与聊天室关联的消息用户的图像

对基本问题道歉。 我设法遵循教程,并在SO社区的帮助下设法与Action Cable建立群聊,并学习了这样做的负担。 但是,我正在尝试使用特定的html页面 – 与current_users Chatrooms相关联的Messaging User的图像。 我已经能够拉出与当前用户相关的聊天室以及这些聊天室中提供的最后一条消息。 我试过以下但这只给了我当前聊天室中我的消息用户的图像。 我在下面列出了所有相关代码。

show.html.erb(在我的聊天室html文件夹中)

 

Chatroom.rb

 class Chatroom {where(direct_message: false) } scope :direct_messages, ->{ where(direct_message: true) } def self.direct_message_for_users(users) user_ids = users.map(&:username).sort name = "#{user_ids.join(" and ")}" if chatroom = direct_messages.where(name: name).first chatroom else chatroom = new(name: name, direct_message: true) chatroom.users = users chatroom.save chatroom end end end 

chatroomuser.rb

 class Chatroomuser < ApplicationRecord belongs_to :user belongs_to :chatroom end 

message.rb

 class Message < ApplicationRecord belongs_to :user belongs_to :chatroom end 

User.rb

 class User < ApplicationRecord devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable, :omniauthable, omniauth_providers: [:facebook] cattr_accessor :current_user has_many :chatroomusers has_many :chatrooms, through: :chatroomusers has_many :messages end 

Chatrooms_controller.rb

 class ChatroomsController < ApplicationController before_action :set_chatroom, only: [:show, :edit, :update, :destroy] def index @chatrooms = Chatroom.public_channels end def show @messages = @chatroom.messages.order(created_at: :desc).limit(100).reverse end private def set_chatroom @chatroom = Chatroom.find(params[:id]) end def chatroom_params params.require(:chatroom).permit(:name) end end 

直接消息控制器

 class DirectMessagesController < ApplicationController before_action :authenticate_user! def show users = [current_user, User.find(params[:id])] @messageduser = User.find(params[:id]) @chatroom = Chatroom.direct_message_for_users(users) @chatroomall = current_user.chatrooms @messages = @chatroom.messages.order(created_at: :desc).limit(100).reverse @messagelast = @chatroom.messages.last(1) render "chatrooms/show" end private def chatroomuserlocator @chatroomlocator = Chatroom.find(params[:chatroom_id]) end end 

ChatroomUsers控制器

 class ChatroomUsersController < ApplicationController before_action :authenticate_user! before_action :set_chatroom def create @chatroomuser = @chatroom.chatroomusers.where(user_id: current_user.id).first_or_create redirect_to @chatroom end def destroy @chatroomuser = @chatroom.chatroomusers.where(user_id: current_user.id).destroy_all redirect_to chatrooms_path end private def set_chatroom @chatroom = Chatroom.find(params[:chatroom_id]) end end 

Schema.rb

 create_table "chatrooms", force: :cascade do |t| t.string "name" t.datetime "created_at", null: false t.datetime "updated_at", null: false t.boolean "direct_message" end create_table "chatroomusers", force: :cascade do |t| t.integer "user_id" t.integer "chatroom_id" t.datetime "created_at", null: false t.datetime "updated_at", null: false t.datetime "last_read_at" t.index ["chatroom_id"], name: "index_chatroomusers_on_chatroom_id" t.index ["user_id"], name: "index_chatroomusers_on_user_id" end create_table "create_messages", force: :cascade do |t| t.integer "user_id" t.integer "chatroom_id" t.text "body" t.datetime "created_at", null: false t.datetime "updated_at", null: false t.index ["chatroom_id"], name: "index_create_messages_on_chatroom_id" t.index ["user_id"], name: "index_create_messages_on_user_id" end 

所以你有一个你想要的聊天室列表

 <% @chatroomall.each do |chatroom| %> 

所以在循环内你可以访问一个名为chatroom的变量中的每个特定聊天室吗?

那么对于每个特定的聊天室,那么你想要显示最后一个消息用户的头像吗?

 <% @chatroomall.each do |chatroom| %> <%= image_tag chatroom.messages.last.user.avatar.url(:thumb), id: "css-style3" %> 

等等…

应该做的伎俩

UPDATE

当然,有时可能会在聊天室中没有消息,因此需要通过简单的检查来处理

 <% @chatroomall.each do |chatroom| %> <% if chatroom.messages.empty? %> No messages in this chatroom <% else %> <%= image_tag chatroom.messages.last.user.avatar.url(:thumb), id: "css-style3" unless chatroom.messages.last.user.blank?%> //Note additional check for a user <% end %> 

您可能希望使用ul li标签或其他内容来设置上述样式,这样您就不会在一条长行上获得列表

将这样的逻辑放在模型中的另一个原因就是你可以在一个地方完成所有检查,并且在获取信息的时候永远不必担心它在聊天室模型上寻找类似于last_user方法的信息只是一个简单的chatroom.last_user.name unless last_user.blank? 只要你需要它就会做到。 更干净,这是Rails的方式!

结束更新

但是你应该通过创建一个名为last_message的命名范围或方法将一些重复的代码重构到你的聊天室模型中

 def last_message messages.last end 

这样,如果你需要重构你获取聊天室的最后一条消息的方式,你只有一个地方可以这样做,那么你可以替换像

 chatroom.messages.last(1).pluck(:created_at) 

 chatroom.last_message.pluck(:created_at) 

请注意我的代码未经测试且可能存在错误,但应该给出正确的想法,如果我误解了您的问题,那么请说明,如果您能够清楚地定义,那么破解您的实际要求并不容易你的实际需求,你通常会发现答案是盯着你,例如我把你的问题解释为

“如何在当前登录用户的每个聊天室中显示最后一个消息用户的头像?”

在尝试找出更复杂的逻辑时,流程图或结构化图表通常很有用,看看是否可以像https://en.wikipedia.org/wiki/Jackson_structured_programming