首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用action cable向rails中的单个用户发送通知

如何使用action cable向rails中的单个用户发送通知
EN

Stack Overflow用户
提问于 2020-04-27 04:57:05
回答 1查看 418关注 0票数 0

其实我在动感线缆上有点迷惑。问题是,我想要发送通知给每个特定的用户,他们的id出现在我的payment table.And中,我在控制器操作中多次尝试ActionCable.server.broadcast。但它显示AbstractController::DoubleRenderError。

这是我的频道

代码语言:javascript
复制
class WebNotificationsChannel < ApplicationCable::Channel
  def subscribed
    # stream_from "some_channel"
    stream_from "notifications:#{current_user.id}"
  end
end

控制器操作部件

代码语言:javascript
复制
@payments = Payment.all
@payments.each do |payment|
  ActionCable.server.broadcast "notifications:#{payment.user_id}", html: notification_render(notification) , count: unseen_notification_number
end

注: payment belongs_to用户

渲染部分的方法

代码语言:javascript
复制
def notification_render(notification)
  render(partial: 'homepage/notification', locals: {notification: notification})
end

错误跟踪部分控制台

代码语言:javascript
复制
AbstractController::DoubleRenderError (Render and/or redirect were called multiple times in this action. Please note that you may only call render OR redirect, and at most once per action. Also note that neither redirect nor render terminate execution of the action, so if you want to exit an action after redirecting, you need to do something like "redirect_to(...) and return".):
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-04-27 05:46:51

DoubleRenderError是由于在迭代器中使用了render。我对Action Cable不是很熟悉,但更改为render_to_string可能会有所帮助。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61447993

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档