You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Rails中为各命名空间创建Action Cable通知频道

给Action Cable频道创建命名空间的分步方案

嘿,刚好适配你项目里的Trainee、Trainer、Manager三个角色场景,咱们一步步来配置Action Cable的命名空间,确保每个角色的通知逻辑完全独立:

1. 创建带命名空间的频道文件

首先在app/channels目录下为每个命名空间创建子文件夹,然后在各自文件夹里定义频道类:

Trainee 频道示例(app/channels/trainee/notifications_channel.rb)

module Trainee
  class NotificationsChannel < ApplicationCable::Channel
    def subscribed
      # 验证当前用户是否为Trainee,确保权限安全
      if current_user.is_a?(Trainee)
        # 为当前Trainee用户创建专属数据流
        stream_for current_user
      else
        reject
      end
    end

    def unsubscribed
      # 可选:断开连接时的资源清理逻辑
    end

    def receive(data)
      # 处理前端发来的双向通信消息(按需实现)
      puts "Trainee received message: #{data}"
    end
  end
end

Trainer 和 Manager 频道同理

分别创建app/channels/trainer/notifications_channel.rb和app/channels/manager/notifications_channel.rb,替换模块名和权限验证逻辑(比如current_user.is_a?(Trainer))即可。

2. 配置Action Cable路由

在config/routes.rb里为每个命名空间注册频道:

Rails.application.routes.draw do
  # 基础Action Cable挂载
  mount ActionCable.server => '/cable'

  # Trainee命名空间频道注册
  namespace :trainee do
    resources :notifications, only: [] do
      collection do
        subscribe :notifications_channel
      end
    end
  end

  # Trainer命名空间频道注册
  namespace :trainer do
    resources :notifications, only: [] do
      collection do
        subscribe :notifications_channel
      end
    end
  end

  # Manager命名空间频道注册
  namespace :manager do
    resources :notifications, only: [] do
      collection do
        subscribe :notifications_channel
      end
    end
  end
end

3. 前端连接对应命名空间的频道

在前端JavaScript文件里(比如app/javascript/channels/trainee_notifications.js),连接到Trainee的命名空间频道,并处理专属通知内容:

Trainee前端示例

import consumer from "./consumer"

consumer.subscriptions.create({ channel: "Trainee::NotificationsChannel" }, {
  connected() {
    console.log("Trainee notifications connected!")
  },

  disconnected() {
    // 可选:断开连接时的提示或状态更新
  },

  received(data) {
    // 渲染Trainee专属通知模板
    const notificationElement = document.createElement('div')
    notificationElement.className = 'trainee-notification'
    notificationElement.innerHTML = `
      <h4>${data.title}</h4>
      <p>${data.content}</p>
      <span class="time">${data.created_at}</span>
    `
    document.getElementById('trainee-notification-list').appendChild(notificationElement)
  }
})

Trainer和Manager前端同理

分别创建对应的JS文件,替换频道名(Trainer::NotificationsChannel、Manager::NotificationsChannel),并渲染各自角色的专属模板即可。

4. 后端发送命名空间专属通知

在控制器或后台任务中,给对应角色的频道定向广播通知:

给Trainee发送通知示例

# 比如在培训任务创建后
Trainee::NotificationsChannel.broadcast_to(
  current_trainee,
  title: "新的培训任务已分配",
  content: "请在3天内完成《基础技能入门》模块学习",
  created_at: Time.current.strftime("%Y-%m-%d %H:%M")
)

Trainer和Manager的广播同理

用Trainer::NotificationsChannel.broadcast_to(...)和Manager::NotificationsChannel.broadcast_to(...)发送各自的通知内容即可。

这样配置后,三个命名空间的频道完全独立,各自的通知逻辑、模板都不会互相干扰,完美匹配你的项目需求!

内容的提问来源于stack exchange,提问作者Trần Hồng

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 09:18:47