首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >TypeError:未定义App.cable

TypeError:未定义App.cable
EN

Stack Overflow用户
提问于 2016-03-03 20:44:14
回答 3查看 1.3K关注 0票数 2

我在我的机器上运行ActionCable时遇到问题。

在assets/javascripts/channels中我有两个文件:

index.coffee

代码语言:javascript
复制
#= require action_cable
#= require_self
#= require_tree .

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

和question.coffee

代码语言:javascript
复制
App.question = App.cable.subscriptions.create "QuestionChannel",
  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

  follow: ->
    @perform 'follow'

  unfollow: ->
    @perform 'unfollow'

我的application.js文件如下所示:

代码语言:javascript
复制
// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// compiled file.
//
// Read Sprockets README (https://github.com/sstephenson/sprockets#sprockets-directives) for details
// about supported directives.
//
//= require jquery
//= require jquery_ujs
//= require bootstrap-sprockets
//= require icheck
//= require homer/scripts
//= require Chart
//= require excanvas
//= require channels
//= require_tree .

当我启动cable服务器和rails服务器并访问firefox控制台中的localhost:3000时,我看到两个错误:

代码语言:javascript
复制
SyntaxError: An invalid or illegal string was specified


this.webSocket = new WebSocket(this.consumer.url);

代码语言:javascript
复制
TypeError: App.cable is undefined


App.question = App.cable.subscriptions.create("QuestionChannel", {

我使用的是Rails5beta3,我该怎么解决这个问题呢?

EN

回答 3

Stack Overflow用户

发布于 2016-03-04 03:38:56

ActionCable.createConsumer需要一个参数-操作电缆服务器的地址。

代码语言:javascript
复制
@App = {}
App.cable = ActionCable.createConsumer("ws://cable.example.com")

如果您缺少此参数,则this.customer.url是未定义的。

票数 1
EN

Stack Overflow用户

发布于 2016-04-05 20:11:26

确保你有

代码语言:javascript
复制
<%= action_cable_meta_tag %>

在您的应用程序布局中的某个位置。

默认情况下,Rails生成并负责将电缆URL提供给Action cable,这样您就不必将其传递给

代码语言:javascript
复制
ActionCable.createConsumer()

票数 1
EN

Stack Overflow用户

发布于 2019-01-07 20:06:30

我也有同样的问题。我把我的服务器换成了puma,真是太棒了!

您可以通过添加gem 'puma'并运行bundle进行更改。

另外,确保在development.rb文件中使用config.action_cable.url = "ws://localhost:3000/cable"。祝好运

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35772546

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档