2017-07-01 48 views
1

Rails 5のActionCableが新しくなりました。私はRailsアプリケーションのチャットをしようとしています。コンソールからテストメッセージを送信しようとすると、App.room.speak('Test!')が表示されますが、エラーが発生します。Rails 5 ActionCableアプリケーションが定義されていません

Uncaught ReferenceError: App is not defined at :1:1

room.coffe

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) -> 
    # Called when there's incoming data on the websocket for this channel 
    $('message').append data 

    speak: (message)-> 
    @perform 'speak', message: message 

room_channel.rb

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

    def unsubscribed 
     # Any cleanup needed when channel is unsubscribed 
    end 

    def speak (data) 
    Message.new content: data['message'], user: current_user 
    end 
end 

broadcast_message_job.rb

class BroadcastMessageJobJob < ApplicationJob 
    queue_as :default 

    def perform(meesage) 
    ActionCable.server.broadcast 'room_channel', render_message(message) 
    end 

    private 

    def render_message(message) 
    ApplicationController.renderer.render message 
    end 
end 

cable.js

(function() { 
    this.App || (this.App = {}); 

    App.cable = ActionCable.createConsumer(); 

}).call(this); 

routes.rbを

mount ActionCable.server => '/cable' 

答えて

1

あなたはcable.jsファイルにrequire_selfを追加したことを確認してください:あなたがしている場合、これは、この同じcable.jsファイルからコンテンツを追加します

// app/assets/javascripts/cable.js 
//= require action_cable 
//= require_self 
//= require_tree ./channels 

(function() { 
    this.App || (this.App = {}); 
    App.cable = ActionCable.createConsumer(); 
}).call(this); 

他のファイルにAppを定義した場合は、 "require"セクションに追加する必要があります。

関連する問題