【发布时间】:2021-01-28 09:16:42
【问题描述】:
我对 Ruby 有点陌生,我正在构建一个非常简单的 Rails 应用程序,它允许使用 Action Cable 进行聊天类型的功能。我正在遵循的教程没有使用 rails 6,因此在构建它时它有点不同,但似乎除了下面的 jQuery 来启动 Action Cable 连接之外,此时一切都在工作。我收到一个控制台错误,在 cable.js 文件中显示“未捕获的 ReferenceError:未定义应用程序”。这是有史以来第一次使用动作电缆和导轨,因此非常感谢您的帮助。
这是我的 git 存储库的链接:https://github.com/scottlandes1217/Hubbubb
cable.js
// Action Cable provides the framework to deal with WebSockets in Rails.
// You can generate new channels where WebSocket features live using the `rails generate channel` command.
//
//= require action_cable
//= require_self
//= require_tree ./channels
(function() {
this.App || (this.App = {});
App.cable = ActionCable.createConsumer();
}).call(this);
forum_channel.js
$(function() {
$('[data-channel-subscribe="forum"]').each(function(index, element) {
var $element = $(element),
forum_id = $element.data('forum-id')
messageTemplate = $('[data-role="message-template"]');
$element.animate({ scrollTop: $element.prop("scrollHeight")}, 1000)
App.cable.subscriptions.create(
{
channel: "ForumChannel",
forum: forum_id
},
{
received: function(data) {
var content = messageTemplate.children().clone(true, true);
content.find('[data-role="user-avatar"]').attr('src', data.user_avatar_url);
content.find('[data-role="message-text"]').text(data.message);
content.find('[data-role="message-date"]').text(data.updated_at);
$element.append(content);
$element.animate({ scrollTop: $element.prop("scrollHeight")}, 1000);
}
}
);
});
});
【问题讨论】:
-
顺便说一句 - 如果有帮助的话,这就是我正在学习的教程iridakos.com/programming/2019/04/04/…
标签: jquery ruby-on-rails ruby-on-rails-6 actioncable