【发布时间】:2017-05-21 02:47:36
【问题描述】:
为基本问题道歉。我设法遵循教程并使用 Action Cable 建立了一个群聊,并从中学到了很多东西。但是,我试图打开一个特定的 html 页面——与当前用户关联的所有聊天室。我尝试了以下操作,但这仅给了我当前显示的 direct_message 页面。我在下面列出了所有相关代码。
show.html.erb(在我的聊天室 html 文件夹中)
<% @chatroomall.each do |chatroom| %>
<div>
<%= @chatroom.name %>
</div>
<% end %>
Rails 日志
Chatroom Load (0.4ms) SELECT "chatrooms".* FROM "chatrooms" WHERE "chatrooms"."direct_message" = ? AND "chatrooms"."name" = ? ORDER BY "chatrooms"."id" ASC LIMIT ? [["direct_message", true], ["name", "Monica and Rodrigo Williams"], ["LIMIT", 1]]
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 = Chatroom.all
@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
【问题讨论】:
-
聊天室用户的架构是什么样的?也许您没有正确命名命名约定和活动记录?您正在使用连接表 (chatroomusers) 那么为什么在 rails 生成的 SQL 查询中没有连接呢?如果 chatroomusers 有一个 user_id 和 chat_room_id 他们可能没有正确命名,因为没有生成 SQL 连接。
-
@OneNeptune 非常感谢您的回复。我已经用我的架构和 chatroom_users 控制器更新了这个问题。加入是通过 chatroom.rb 模型使用 name = "#{user_ids.join(" and ")}" 至少我相信的。
-
我已经提供了答案,但如果您仍然在苦苦挣扎,请随时打开聊天,因为我认为您在一些基本的 Rails 概念上苦苦挣扎,我很乐意提供帮助
标签: ruby-on-rails ruby ruby-on-rails-5 actioncable