通过Actioncable

时间:2017-05-21 02:47:36

标签: ruby-on-rails ruby ruby-on-rails-5 actioncable

为基本问题道歉。我设法遵循教程并使用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

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 = 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

1 个答案:

答案 0 :(得分:1)

class User < ApplicationRecord中,您有以下声明

has_many :chatrooms, through: :chatroomusers

这允许User类的实例获取用户所关联的所有聊天室。

鉴于你要求

  

与当前用户相关的所有聊天室

并在您的视图模板中

<% @chatroomall.each do |chatroom| %>
<div>
<%= @chatroom.name %>
</div>
<% end %>

您需要修改@ chatroom.name以通过@chatroomall.each do |chatroom|引用本地聊天室变量设置,方法是将代码更改为

<%= chatroom.name %>

请注意,您现在引用的是本地变量,而不是控制器的全局@chatroom设置

您可以通过修改show动作来设置控制器show action以提供实例变量@chatroomall

def show
    users = [current_user, User.find(params[:id])]
    @messageduser = User.find(params[:id])
    @chatroom = Chatroom.direct_message_for_users(users)        
    #@chatroomall = Chatroom.all Change this to
    @chatroomall = current_user.chatrooms
    @messages = @chatroom.messages.order(created_at: :desc).limit(100).reverse
    @messagelast = @chatroom.messages.last(1)
    render "chatrooms/show"
end

请注意更改

#@chatroomall = Chatroom.all Change this to
@chatroomall = current_user.chatrooms

或将模板更改为循环通过current_user.chatrooms

**更新** 这假设你有一个current_user。如果当前登录的用户实例的名称不是current_user,则将current_user更改为匹配