如何解决Rails ActionCable 不会将新内容流式传输到索引页面
你好,我是 Rails 新手,所以我尝试在索引中实时显示我的票证。对不起,我的英语不好,我尝试用其他方式解释它......当某些用户创建票证时,我希望如果有人打开了索引页面,它将自动显示新票证,而无需手动重新加载索引页。
我遵循了本教程:https://dev.to/nkemjiks/simple-chatroom-with-rails-6-and-actioncable-3bc3 只是我没有用脚架创建我的模型,我自己做的。
我不明白我做错了什么:(谢谢你的帮助
型号:
class KitchenTicket < ApplicationRecord
belongs_to :user
after_create_commit { TicketBroadcastJob.perform_later self }
after_update_commit { TicketBroadcastJob.perform_later self }
after_destroy { TicketBroadcastJob.perform_later self }
end
TicketBroadcastJob:
class TicketBroadcastJob < ApplicationJob
queue_as :default
def perform(ticket)
ActionCable.server.broadcast 'ticket_channel',item: render(ticket)
end
private
def render(ticket)
ApplicationController.renderer.render(partial: 'kitchen_tickets/tickets',locals: {ticket: ticket})
end
end
票务通道:
class TicketChannel < ApplicationCable::Channel
def subscribed
stream_from 'ticket_channel'
end
def unsubscribed
# Any cleanup needed when channel is unsubscribed
end
end
ticket_channel.js:
import consumer from "./consumer"
consumer.subscriptions.create("TicketChannel",{
connected() {
// Called when the subscription is ready for use on the server
},disconnected() {
// Called when the subscription has been terminated by the server
},received(data) {
// Called when there's incoming data on the websocket for this channel
location.reload();
}
});
部分:_tickets.html.erb:
<table class="table">
<thead>
<tr>
<th scope="col">Nutzer</th>
<th scope="col">Betreff</th>
<th scope="col">Nachricht</th>
<th scope="col">Status</th>
</tr>
</thead>
<tbody>
<% @kitchen_tickets.each do |ticket| %>
<tr>
<td><%= ticket.user.name %></td>
<td><%= ticket.subject %></td>
<td><%= ticket.message %></td>
<td><%= "Offen" if !ticket.status %></td>
</tr>
<% end %>
</table>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。