0

I'm trying to display a simple chat between users. I would like to design it like iMessages, to differentiate the senders, but I just can't figure out how to do so. No matter what I try, it always seems like message.sender == current_user , even for the receiver

Here's the js.erb for private pub:

<% publish_to "/comments/create" do %>
 var new_comment = $("<%= escape_javascript(render 'projects/comment', comment: @comment) %>");
  var comment_box = $('.feedback-index');
  new_comment.appendTo(comment_box);

  $('#comment_content').val('');
  $(' .fa-comment').text(" <%= @comment.project.comments.size %>");
 $('#messagesbox').scrollTop($('#messagesbox').prop("scrollHeight"));

<% end %>

Now, the tricky part, in my _message.html.erb, comment.sender == current_user is always true. I tried to implement two designs, based on the current user, using a helper :

HTML:

<div class="col-xs-8 col-sm-9 the-comment <%= self_or_other(comment) %>">

Helper:

module MessagesHelper
  def self_or_other(message)
    message.user == current_user ? "current-user-message" : ""
  end
end

unfortunately, my div always get the "current-user-message" class.

Am I missing something ?

Pierre Olivier Tran
  • 1,669
  • 4
  • 24
  • 40

1 Answers1

0

Well, I found a clean solution to this problem: Somewhere in the wrapper, add an input including your current_user id

<input type="hidden" value="<%= current_user.id %>" name="sender-id" id="senderid">

Then, in your js publish, retrieve it, and voila :)

<% publish_to whatever_you_want  do %>
    sender = $('#senderid').val();
    if (sender == "<%= current_user.id %>") {
       // do your stuff
    }
<% end %>
Pierre Olivier Tran
  • 1,669
  • 4
  • 24
  • 40