【问题标题】:learn enough action cable tutorial not working学习足够的动作电缆教程不起作用
【发布时间】:2017-08-19 08:25:27
【问题描述】:

我正在编写 Hartl 的教程 Learn Enough Action Cable,但我一直停留在第 4.1 节。我复制并粘贴了教程中的代码以确保不会出现带有 data.content 的弹出窗口。

消息控制器:

.
.
def create
   message = current_user.messages.build(message_params)
   if message.save
      ActionCable.server.broadcast 'room_channel',
                              content:  message.content,
                              username: message.user.username
  end
end

room_channel.rb:

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

    def unsubscribed
    end
 end

routes.rb:

Rails.application.routes.draw do
root 'messages#index'
resources :users
resources :messages
get    '/login',   to: 'sessions#new'
post   '/login',   to: 'sessions#create'
delete '/logout',  to: 'sessions#destroy'

mount ActionCable.server, at: '/cable'
end

room.coffee:

App.room = App.cable.subscriptions.create "RoomChannel",
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) ->
alert(data.content)

本教程的所有其他部分都可以正常使用弹出窗口来显示 javascript 警报。在我的服务器日志中它说:

Started GET "/cable" for 127.0.0.1 at 2017-03-26 13:59:12 -0400
Started GET "/cable/" [WebSocket] for 127.0.0.1 at 2017-03-26 13:59:12 -0400
Successfully upgraded to WebSocket (REQUEST_METHOD: GET,HTTP_CONNECTION:     keep-alive, Upgrade, HTTP_UPGRADE: websocket)
RoomChannel is transmitting the subscription confirmation
RoomChannel is streaming from room_channel
Finished "/cable/" [WebSocket] for 127.0.0.1 at 2017-03-26 14:06:26 -0400
RoomChannel stopped streaming from room_channel
Started GET "/messages" for 127.0.0.1 at 2017-03-26 14:06:26 -0400
Processing by MessagesController#index as HTML
User Load (0.2ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ?   LIMIT ?  [["id", 1], ["LIMIT", 1]]
Message Load (0.2ms)  SELECT  "messages".* FROM "messages" ORDER BY        "messages"."created_at" DESC LIMIT ?  [["LIMIT", 50]]
 Rendering messages/index.html.erb within layouts/application
Rendered messages/_messages.html.erb (0.6ms)
Rendered messages/_message_form.html.erb (2.0ms)
Rendered messages/index.html.erb within layouts/application (4.5ms)
Rendered layouts/_logo.html.erb (0.4ms)
Rendered layouts/_header.html.erb (2.0ms)
Completed 200 OK in 71ms (Views: 36.4ms | ActiveRecord: 1.5ms)

【问题讨论】:

  • 您的问题解决了吗?据我所知,这个问题。

标签: ruby-on-rails ruby-on-rails-5 actioncable


【解决方案1】:

我也遇到了同样的问题。

您还可以看到,消息正在被广播,但在客户端(即 room.coffee)没有收到

所以问题在于 CoffeeScript 没有正确的左括号和右括号。所以它的工作原理是适当的缩进。

当你从网上复制代码时,缩进不正确。

在下面的代码中,确保received: data 之后只有 2 个空格,如下所示:

received: (data) -> alert(data.content)

alert 语句的开头应该有两个空格。

此外,connectedreceived 之类的函数相对于 App.room = App.cable.subscriptions.create "RoomChannel", 行也应该是 2 个空格

最后你的代码应该是这样的:

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

  disconnected: ->

  received: (data) ->
    alert(data.content)

确保使用空格而不是制表符

【讨论】:

    猜你喜欢
    • 2017-11-27
    • 1970-01-01
    • 2018-04-20
    • 2023-04-07
    • 2020-09-04
    • 1970-01-01
    • 2018-07-29
    • 2016-11-23
    相关资源
    最近更新 更多