Search code examples
ruby-on-railsactioncable

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 . But it shows 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

@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

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

Solution

  • DoubleRenderError is due to the use of render in the iterator. I'm not very familiar with Action Cable, but change to render_to_string might help.