本文介绍了Rails ActionCable &部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

但是,由于 Rails 部分,我得到了用户 avatars 呈现在错误一侧的行为.

However, I'm getting the behavior of user avatars rendering on the wrong side due to a Rails partial.

# conversations/_message.html.erb

<% if current_user.id == message.user_id %>
  <Display Avatar Right>
  </Display Avatar Right>
<% else %>
  <Display Avatar Left>
  </Display Avatar Left>
<% end %>

当消息属于当前用户时,头像需要右侧,否则,头像需要左侧.

When a message belongs to the current user, the avatar needs to be on the right, when not, the avatar needs to be on the left.

在我的 MessagesChannelcreate_message 方法中,这是用户创建新消息时调用的方法.问题是,创建消息时,current_user 是创建消息的用户.这将为 This user ALONE 呈现正确的 HTML.频道的其他用户/订阅者将被播放相同的 HTML...这将导致在第一个屏幕截图中看到的错误.

In the create_message method of my MessagesChannel, this is the method that is called when a user creates a new message. The issue is, when a message is created, current_user is the user who created the message. This will render the correct HTML for This user ALONE. Other users/subscribers to the channel will be broadcasted the SAME HTML... this will result in the bug seen in the first screen shot.

def create_message(message_params)
  message = Message.new context_type: 'Conversation'
  message[:body] = message_params['data']['body']
  message[:context_id] = message_params['data']['conversation_id']
  message[:user_id] = message_params['data']['user_id']
  data = {}
  if message.save
    data[:html] = ApplicationController.render partial: 'conversations/message', locals: { message: message, current_user: current_user }
  else
    p message.errors.full_messages.to_sentence
    data[:html] = "Error: #{message.errors.full_messages.to_sentence}"
  end

  ActionCable.server.broadcast('room', data)
end

问题在于

# conversations/_message.html.erb

if current_user.id == message.user_id 将始终返回 true,即使对于其他用户(订阅此频道的用户),头像也会显示在右侧.

if current_user.id == message.user_id will always return true and the avatar displays on the right even for other users(users who are subscribed to this channel.)

另一件事是,我不想触及部分中的行为,因为对于发送消息的用户来说,从 MessagesChannel 发回的 html 是正确(它在右侧呈现头像.)

Another thing is, I don't want to touch the behavior in the partial because for the user who sent the message, the html sent back from the MessagesChannel is correct(it renders the avatar on the right.)

有人知道我仍然可以使用此 _message 部分的解决方法吗?谢谢!

Anyone know a workaround where I can still use this _message partial? Thanks!

推荐答案

hej,您的部分在广播之前被渲染.broadcast_to 方法传输一个字符串,它是部分渲染的 html.因此,当您呈现部分时,current_user 是发送消息的人,因此它以发件人看到部分的方式呈现.一个解决方案可能是传递一个布尔变量sender"到你的部分,即

hej,your partial is being rendered before being broadcasted. the broadcast_to method transmits a string which is the rendered html of the partial. as a result, when you render your partial, the current_user is the one who sends the message and therefore it is rendered the way the sender sees the partial.a solution could be to pass a boolean variable "sender" to your partial, i.e.

<% if sender %>
  <Display Avatar Right>
  </Display Avatar Right>
<% else %>
  <Display Avatar Left>
  </Display Avatar Left>
<% end %>

然后您在数据对象和发送者 ID 中同时传输发送者和接收者部分,例如

then you transmit both the sender and the receiver partials in the data object and the sender Id, e.g.

data[:htmlSender] = ApplicationController.render partial: 'conversations/message', locals: { message: message, sender: true }

data[:htmlReceiver] = ApplicationController.render partial: 'conversations/message', locals: { message: message, sender: false, }数据[:senderId] = current_user.id.to_s

data[:htmlReceiver] = ApplicationController.render partial: 'conversations/message', locals: { message: message, sender: false, }data[:senderId] = current_user.id.to_s

然后在收到的(数据)回调中设置条件,将数据对象中的 senderId 与 current_user id 进行比较,然后插入 data.htmlSender 或 data.htmlReceiver.

then you make a conditional in the received(data) callback where you compare the senderId from your data Object with the current_user id and insert either data.htmlSender or data.htmlReceiver.

这篇关于Rails ActionCable &amp;部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-07 06:02