I’m building a chat system in my app and using Devise for auth, but I’m running into an issue with the styling of the messages after a user posts one.
What I want is for it to be the same as any messaging app where the current_user
s sent messages appear on the right and the other messages on the left.
My message partial:
<%= turbo_frame_tag dom_id(message) do %>
<div class="<%= message.user == current_user ? "m-right" : "m-left" %>">
....
</div>
<% end %>
When I have the current_user
condition in the div class, it breaks my turbo_stream functionality and throws this error,
[ActiveJob] [Turbo::Streams::ActionBroadcastJob] [46dd61a9-f770-4a86-ab93-8f2b29777b91] Error performing Turbo::Streams::ActionBroadcastJob (Job ID: 46dd61a9-f770-4a86-ab93-8f2b29777b91) from Async(default) in 7.23ms: ActionView::Template::Error (Devise could not find the
Warden::Proxy
instance on your request environment.
After searching for this error, I came across a few threads that led me to making current_user
into a user
variable on my _message
partial.
<%= render 'message', message: message, user: current_user %>
and changed my broadcast in my model to be
after_create_commit { broadcast_append_later_to self.conversation, target: "conversation_#{self.conversation.id}", partial: 'messages/message', locals: { message: self, user: curent_user } }
This throws a NameError
NameError - undefined local variable or method `current_user’
If I try and move my broadcast to my controller instead
def create
@message = @conversation.messages.build(message_params)
if @message.save
@message.broadcast_append_later_to @conversation, target: "conversation_#{@conversation.id}", partial: 'messages/message', locals: { message: @message, user: current_user }
respond_to do |format|
format.turbo_stream {}
format.html { redirect_to @conversation }
end
end
end
The broadcast works correctly, but it sets current_user
to the user that sent the message (User A) so when User B receives the message, it is styled incorrectly because user
in
<%= message.user == user ? "m-left" : "m-right" %>
gets set by the controller to the person (current_user
) that posted the message
Has anyone gotten the functionality Im looking for to work correctly?
Thanks.