So, I have this use case when I pass a message in the controller (with ApplicationController.renderer), which is then passed to multiple users. Broadcasting is also performed inside the same controller. Both of these actions are triggered when performing an update for a specific object.
The problem is that I need to access the current_user object inside this visualized view, and of course I cannot display it with the current user as a local variable, because then the message will be sent with the user who broadcast the message and not the end The user who sees this view.
So, after reading a few blog posts and Rails documents, I set up authentication using cookies, which will be supported by the action cable.
My question is: how can I access the end user object (current_user) inside the rendered view?
Currently, my connection class is as follows. However, how can I visualize this 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 is as follows:
(...) 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 (....)
Basically, my ultimate goal is to access the logged_user object after the message has been passed to it.
thanks