接收未加载Coffee脚本的数据

时间:2019-04-04 01:55:08

标签: ruby-on-rails actioncable

我无法从javascripts / channels / chatrooms.coffee中加载咖啡脚本

在加载javascripts / chatrooms.coffee时。

在channels / chatrooms.coffee中,我有一个具有console.log(data)的receive:(data)函数,不会记录接收到的数据。

我遵循了动作电缆指南,并且可以正常工作,但是我丢失了提交。我不记得更改任何内容。

class MessageRelayJob < ApplicationJob
  queue_as :default

  def perform(message)
    ActionCable.server.broadcast "chatrooms:#{message.chatroom.id}", {
      username: message.user.username,
      body: GroupmessagesController.render(message),
      chatroom_id: message.chatroom.id
    }
    # Do something later
  end
end
App.chatrooms = App.cable.subscriptions.create "ChatroomsChannel",
  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) ->
    console.log(data)
    active_chatroom = $("[data-behavior='messages'][data-chatroom-id='#{data.chatroom_id}']").append("<div><strong>#{data.username}:</strong> #{data.message}</div>")
    if active_chatroom.length > 0
      active_chatroom.append("<div><strong>#{data.username}:</strong> #{data.body}</div>")
    else
      $("[data-behavior='chatroom-link'][data-chatroom-id='#{data.chatroom_id}']").css("font-weight", "bold")
    # Called when there's incoming data on the websocket for this channel

  send_message: (chatroom_id, body) ->
    @perform "send_message", {chatroom_id: chatroom_id, body: body}

<div data-behavior='messages' data-chatroom-id='<%= @chatroom.id %>' class=messages style="margin-left: 140px; border-left: 1px black solid; padding-left: 2px;">
    <% @chatroom.groupmessages.order(created_at: :desc).limit(100).reverse.each do |groupmessage| %>
        <%= render groupmessage %>
    <% end %>
</div>

服务器到达此处以广播数据的消息中继作业

[ActiveJob] [MessageRelayJob] [dabb113e-516d-49c2-8cbe-2913506ef60d] [ActionCable] Broadcasting to chatrooms:1: {:username=>nil, :body=>"<div><strong>:</strong>hi</div>", :chatroom_id=>1}
[ActiveJob] [MessageRelayJob] [dabb113e-516d-49c2-8cbe-2913506ef60d] Performed MessageRelayJob (Job ID: dabb113e-516d-49c2-8cbe-2913506ef60d) from Async(default) in 47.01ms

0 个答案:

没有答案