有关actioncable中未订阅的频道流的信息

问题描述

在导轨中实现ActionCable通道。

def subscribed

  room = Room.find_by_id(params[:room_id])
  stream_for room
end

def unsubscribed
  room_id = room.try(:id) || params[:room_id] 
end

我们可以通过取消订阅方法获取room_id或房间(ActiveRecord obj)信息吗?

解决方法

subscribed方法中使用instance(@)变量,我们可以在unsubscribed方法中获取相同的对象

def subscribed
  @room = Room.find_by_id(params[:room_id])
  stream_for @room
end

def unsubscribed
  puts "room => #{@room}" # Here we will get the same room object
end