【发布时间】:2019-07-04 04:55:42
【问题描述】:
我有一个基本的聊天应用程序,它通过 ActionCable 发送消息。我正在尝试根据谁发送消息对消息进行左/右排序,就像 Facebook 的信使应用程序一样。 我正在尝试使用我发送的部分(_message.html.erb)中的基本逻辑来实现它:
<%if message.user_id == current_user.id %>
//message is to the right with inline CSS
<% else°%>
// message is to the left with inline CSS
<% end°%>
如果有人重新加载页面,它工作得很好,但是当使用应用程序时,通过 ActionCable 发送的所有新消息都会发送到右侧。
我检查了发送的部分,似乎
<%if message.user_id == current_user.id %>
在用户端运行,然后总是发送部分的第一部分,而不是整个 _message.html.erb。有没有办法代替发送整个部分并确保
<%if message.user_id == current_user.id %>
当所有订阅用户都收到消息时运行?
消息控制器:
if @message.save
RoomChannel.broadcast_to @room.id, message:
MessagesController.render(partial: 'messages/message', locals: {message:
@message, current_user: current_user})
room.js:
received: function (data) {
$('.messages').append(data.message);
}
如果需要代码的任何其他部分,请询问或:https://github.com/TTARJAN/chatApp
【问题讨论】:
标签: ruby-on-rails-5 actioncable rails-activejob