So I have this use case where I render a message in a controller (with ApplicationController.renderer) that is then broadcasted to a couple of users. The broadcast also is performed in inside the same controller. Both these actions are triggered when an update to certain object is performed.
The problem is, I need to access the current_user object inside that rendered view, and of course, I can not render it with the current user as a local variable because then the message will be sent with the user that broadcasted the message and not the end user that will see that view.
So, after reading a couple of blog posts and the Rails docs I set the authentication with cookies to be supported by action cable.
My question is: how can I access, inside the rendered view, the object (current_user) of the end user?
Currently, my connection class looks like this. However, how can I render that view with this variable (logged_user)?
module ApplicationCable
class Connection < ActionCable::Connection::Base
identified_by :logged_user
def connect
self.logged_user = User.find_by(id: cookies.signed[:user_id])
end
end
end
My controller looks like this:
(...)
def update
if @poll.update(poll_params)
broadcast_message(render_message(@poll), @poll.id, @poll.room.id)
(...)
end
end
def broadcast_message(poll = {}, poll_id, room_id)
ActionCable.server.broadcast 'room_channel', body: poll, id: poll_id, room_id: room_id
end
def render_message(poll).
if poll.show_at.to_time <= Time.now
ApplicationController.renderer.render(
partial: 'rooms/individual_student_view_poll',
locals: {
poll: poll,
room: @room
})
end
end
(....)
So basically, my ultimate goal is to access the logged_user object after the message is broadcasted to it.
Thanks