TypeError:App.cable未定义

时间:2016-03-03 12:44:14

标签: javascript ruby-on-rails coffeescript actioncable

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

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

index.coffee

#= require action_cable
#= require_self
#= require_tree .

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

和question.coffee

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文件如下所示:

// 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 .

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

SyntaxError: An invalid or illegal string was specified


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

TypeError: App.cable is undefined


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

我正在使用rails 5 beta 3.我该如何解决这个问题?

3 个答案:

答案 0 :(得分:1)

ActionCable.createConsumer需要一个参数 - Action Cable服务器的地址。

@App = {}
App.cable = ActionCable.createConsumer("ws://cable.example.com")

如果您错过了此参数,则this.customer.url未定义。

答案 1 :(得分:1)

确保你有

<%= action_cable_meta_tag %>

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

Rails默认生成,它负责为Action Cable提供有线URL,这样您就不必将其传递给

ActionCable.createConsumer()

答案 2 :(得分:0)

我有同样的问题。我将服务器更改为puma whaaala,它起作用了!

您可以通过添加public Task<SomeClass> FooAsync() { // Asynchronous Code } 并运行gem 'puma'来进行更改。

还要确保您bundle在development.rb文件中。祝你好运