Rails 5 ActionCable 应用未定义
Rails 5 ActionCable App is not defined
我是 Rails 5 中 ActionCable 的新手。我正在尝试为我的 Rails 应用程序聊天。当我尝试通过控制台 App.room.speak('Test!')
发送测试消息时,我收到错误消息。
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'
确保您已将 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);
这将添加同一 cable.js
文件中的内容,如果您在其他文件中定义了 App
,则需要将其添加到 "require"部分。
我是 Rails 5 中 ActionCable 的新手。我正在尝试为我的 Rails 应用程序聊天。当我尝试通过控制台 App.room.speak('Test!')
发送测试消息时,我收到错误消息。
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'
确保您已将 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);
这将添加同一 cable.js
文件中的内容,如果您在其他文件中定义了 App
,则需要将其添加到 "require"部分。