为这个基本问题道歉。我成功地学习了教程,在so社区的帮助下,我成功地与Action构建了一个小组聊天,并从中学到了很多东西。但是,我试图在一个特定的html页面上找到一个与current_users聊天室相关联的消息用户的图像。我已经能够提取与当前用户相关的聊天室,以及在这些聊天室中传递的最后一条消息。我尝试了下面的方法,但是这只给了我当前聊天室中我的消息用户的图像。我在下面列出了我所有的相关代码。
show.html.erb (在我的聊天室html文件夹中)
<% @chatroomall.each do |chatroom| %>
<div>
<%= image_tag @messageduser.avatar.url(:thumb), id: "css-style3" %>
<%= chatroom.name %>
<%= chatroom.messages.last(1).pluck(:created_at) %>
<%= chatroom.messages.last(1).pluck(:body) %>
</div>
<% end %>Chatroom.rb
class Chatroom < ApplicationRecord
has_many :chatroomusers
has_many :users, through: :chatroomusers
has_many :messages
scope :public_channels, ->{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
endchatroomuser.rb
class Chatroomuser < ApplicationRecord
belongs_to :user
belongs_to :chatroom
endmessage.rb
class Message < ApplicationRecord
belongs_to :user
belongs_to :chatroom
endUser.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
endChatrooms_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
endChatroomUsers控制器
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
endSchema.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发布于 2017-05-22 01:34:35
所以你有一个聊天室的列表
<% @chatroomall.each do |chatroom| %>因此,在循环中,您可以访问一个名为聊天室的变量中的每个特定聊天室,对吗?
那么,对于每个特定的聊天室,您想要显示最后一个消息用户的化身,对吗?
<% @chatroomall.each do |chatroom| %>
<%= image_tag chatroom.messages.last.user.avatar.url(:thumb), id: "css-style3" %>等等。
应该做点什么
更新
当然,在某些情况下,聊天室中没有消息,所以这需要通过简单的检查来处理。
<% @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?就可以在需要的时候发挥作用。干净多了,这是铁轨的方式!
端更新
但是,您应该通过创建一个名为last_message的命名范围或方法,将一些重复的代码重构到聊天室模型中。
def last_message
messages.last
end这样,如果您需要重构您获得聊天室最后消息的方式,您只有一个地方可以这样做,那么您可以替换以下内容
chatroom.messages.last(1).pluck(:created_at)使用
chatroom.last_message.pluck(:created_at)注意:我的代码未经测试,可能有错误,但应该给你正确的想法,如果我误解了你的问题,请说,因为你的实际要求很难破译,这对你来说是一个很大的提示,如果你能清楚地定义你的实际需求,你通常会发现答案是直视你的。例如,我把你的问题解释为
“如何在当前登录用户的每个聊天室中显示最后一个消息用户的化身?”
当试图找出更复杂的逻辑时,流程图或结构化图通常是有帮助的,看看你是否可以像programming这样的东西来解决问题。
https://stackoverflow.com/questions/44096732
复制相似问题