如何使用操作电缆向 rails 中的单个用户发送通知
How to send notification to individual user in rails using action cable
实际上我对动作电缆有点困惑。问题是,我想向每个特定用户发送通知,其 ID 出现在我的付款中 table.And 我在控制器操作中尝试 ActionCable.server.broadcast 多次。但它显示 AbstractController::DoubleRenderError .
这是我的频道
class WebNotificationsChannel < ApplicationCable::Channel
def subscribed
# stream_from "some_channel"
stream_from "notifications:#{current_user.id}"
end
end
控制器动作部分
@payments = Payment.all
@payments.each do |payment|
ActionCable.server.broadcast "notifications:#{payment.user_id}", html: notification_render(notification) , count: unseen_notification_number
end
注:付款belongs_to用户
局部渲染方法
def notification_render(notification)
render(partial: 'homepage/notification', locals: {notification: notification})
end
控制台的错误跟踪部分
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
是由于在迭代器中使用了render
。我不太熟悉 Action Cable,但更改为 render_to_string
可能会有所帮助。
实际上我对动作电缆有点困惑。问题是,我想向每个特定用户发送通知,其 ID 出现在我的付款中 table.And 我在控制器操作中尝试 ActionCable.server.broadcast 多次。但它显示 AbstractController::DoubleRenderError .
这是我的频道
class WebNotificationsChannel < ApplicationCable::Channel
def subscribed
# stream_from "some_channel"
stream_from "notifications:#{current_user.id}"
end
end
控制器动作部分
@payments = Payment.all
@payments.each do |payment|
ActionCable.server.broadcast "notifications:#{payment.user_id}", html: notification_render(notification) , count: unseen_notification_number
end
注:付款belongs_to用户
局部渲染方法
def notification_render(notification)
render(partial: 'homepage/notification', locals: {notification: notification})
end
控制台的错误跟踪部分
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
是由于在迭代器中使用了render
。我不太熟悉 Action Cable,但更改为 render_to_string
可能会有所帮助。