disco-6ren">
gpt4 book ai didi

ruby-on-rails - Action 电缆不工作我必须刷新两个选项卡才能看到消息

转载 作者:数据小太阳 更新时间:2023-10-29 07:19:42 24 4
gpt4 key购买 nike

这是 room.coffee 的代码:

App.room = App.cable.subscriptions.create "RoomChannel",
connected: ->
disconnected: ->

received: (data) ->
$('#messages').append "<p>#{data}</p>"

speak: (message) ->
@perform 'speak' , message: message

有线电视.咖啡:

@App ||= {}
App.cable = ActionCable.createConsumer()

房间.咖啡:

$ ->
$messages = $('messages')
$messages.scrollTop $messages.prop('scrollHieght')
$('#message_input').focus()

$(document).on 'keypress','message_input','e'->
if e.keycode == 13 and e.target.value
App.room.speak(e.target.value)
e.target.value = ''
e.preventDefault()

房间 channel :

class RoomChannel < ApplicationCable::Channel
def subscribed
stream_from "room_channel"
end

def unsubscribed
# Any cleanup needed when channel is unsubscribed
end

def speak(data)
message.create content: data['message']
end
end

广播消息:

 def perform(message)
Actioncable.server.broadcast 'room_channel',render_message(message)
end
private
def render_message(message)
ApplicationController.renderer.render_message
end

当创建新消息时,它不会自动加载我浏览器的所有消息,直到页面不再重新加载。

最佳答案

我遇到了类似的问题。尝试在房间 View 中包含 cable.coffee/js。

<%= javascript_include_tag 'cable', 'data-turbolinks-track': 'reload' %>

您还需要将 cable.js 添加到/config/initializers/assets.rb。

Rails.application.config.assets.precompile += %w( cable.js )

关于ruby-on-rails - Action 电缆不工作我必须刷新两个选项卡才能看到消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38460353/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com