I have set up a message broadcast to the private chatroom in Rails. Everything is working except when I add some CSS classes to the message container to identify if I am the sender or the receiver. The broadcast message that was sent did not identify if I am the sender or not. The broadcast message was using the receiver CSS format since it couldn't identify if that user is the sender of the message.
Here is my Message Controller.
class MessagesController < ApplicationController def create @chatroom = Chatroom.find(params[:chatroom_id]) @message = Message.new(message_params) @message.chatroom = @chatroom @message.user = current_user @message.save ChatroomChannel.broadcast_to( @chatroom, render_to_string(partial: "message", locals: { message: @message }) ) authorize @message end private def message_params params.require(:message).permit(:content) endend
Here is the _message.html.erb for showing the messages in the chatroom.
<div id="message-<%= message.id %>"><% if message.user_id != current_user.id %><div class="message-container-left"><div><%= link_to user_path(message.user), class:"text-decoration-none" do %><%= cl_image_tag message.user.avatar.key, class:"avatar-xs" %><% end %></div><div class="message-content-left"><%= message.content %></div></div><% else %><div class="message-container-right"><div class="message-content-right"><%= message.content %></div><div><%= link_to user_path(message.user), class:"text-decoration-none" do %><%= cl_image_tag message.user.avatar.key, class:"avatar-xs" %><% end %></div></div><% end %></div>
I have tried to debug it and found out the problem is this line
<% if message.user_id != current_user.id %>
Does anyone have any idea how to solve this? My excepted result is to make it like Instagram,the sender message is on the right, and the receiver message is on the left. Thank you!