2016-03-03 66 views
2

我在我的機器上運行ActionCable時遇到問題。TypeError:App.cable未定義

在資產/ Java腳本/渠道我有兩個文件:

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 . 

當我開始電纜服務器和軌道服務器和本地主機訪問:在Firefox控制檯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", { 

我使用的是軌道5測試版3.我該如何解決這個?

+1

所以'ActionCable.createConsumer()'必須返回undefined。 – epascarello

+0

當我在Firefox控制檯鍵入'''ActionCable.createConsumer()'''返回'''語法錯誤:指定了無效的或非法的串this.webSocket =新的WebSocket(this.consumer.url);''' –

+0

那麼'this.consumer.url'的價值是什麼? – epascarello

回答

1

ActionCable.createConsumer需要一個參數 - 地址作用電纜服務器。

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

如果你錯過了這個參數,那麼this.customer.url是不確定的。

0

確保你在你的應用程序了佈局

<%= action_cable_meta_tag %> 

地方。

Rails的默認情況下產生,它需要提供電纜的URL作用電纜的照顧,讓您不必將它傳遞給

ActionCable.createConsumer()