简体   繁体   English

如何使用操作电缆向 Rails 中的单个用户发送通知

[英]How to send notification to individual user in rails using action cable

Actually i am bit confuse in action cable.其实我对行动电缆有点困惑。 The issue is, i want to send notification to each specific user whose id present in my payment table.And i try ActionCable.server.broadcast multiple number of times in the controller action.问题是,我想向我的付款表中存在 id 的每个特定用户发送通知。我在 controller 操作中多次尝试 ActionCable.server.broadcast。 But it shows AbstractController::DoubleRenderError.但它显示 AbstractController::DoubleRenderError。

This is my channel这是我的频道

class WebNotificationsChannel < ApplicationCable::Channel
  def subscribed
    # stream_from "some_channel"
    stream_from "notifications:#{current_user.id}"
  end
end

Controller Action Part Controller 动作部分

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

note: payment belongs_to user注意:付款属于_to用户

Method for render partial部分渲染方法

def notification_render(notification)
  render(partial: 'homepage/notification', locals: {notification: notification})
end

Error track part of console控制台的错误跟踪部分

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".):

DoubleRenderError is due to the use of render in the iterator. DoubleRenderError是由于在迭代器中使用了render I'm not very familiar with Action Cable, but change to render_to_string might help.我对 Action Cable 不是很熟悉,但更改为render_to_string可能会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM