【发布时间】:2017-05-21 12:15:43
【问题描述】:
为基本问题道歉。我设法按照教程进行操作,并在 SO 社区的帮助下设法使用 Action Cable 建立了一个群聊,并从中学到了很多东西。但是,我试图拉上一个特定的 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
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
用户.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
【问题讨论】:
标签: ruby-on-rails ruby ruby-on-rails-5 actioncable