[英]jQuery document ready not firing in a Rails 5 App with ActionCable
我刚刚在生产中部署了一个新的rails 5应用程序,但我无法启动文档就绪功能。 应用程序,包括文档就绪语句中的ActionCable连接和rails控制台输出在Chrome,Firefox等本地激活。
我的app / assets / javascripts / 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 any plugin's vendor/assets/javascripts directory 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. JavaScript code in this file should be added after the last require_* statement. // // Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details // about supported directives. // //= require jquery //= require jquery_ujs //= require bootstrap //= require featherlight.min //= require jquery.growl //= require cable //= require base //= require tradewars //= require extensions //= require_tree . // TradeWars.io - application.js file var Mining_Loop_Iterator = false; $(document).ready(function() { console.log("Initializing new TradeWars class..."); TradeWars = new TradeWarsClass(); console.log("Applying click handlers..."); TradeWars.applyClickHandlers(); console.log("Attempting to create WebSocket..."); App.game = App.cable.subscriptions.create({ channel: "GameChannel" }, { connected: function() {}, disconnected: function() {}, received: function(data) { console.log('Received via ActionCable :: ' + data.message.method + ' :'); console.log(data); if (data.message.method == 'disable_mining') { TradeWars.disableMining(data.message); } else if (data.message.method == 'offer_chest') { TradeWars.offerChest(); } else { // Refresh TradeWars.animateDigging(data.message); TradeWars.refresh(data.message); } return true; }, mine: function(clicks_per_second) { return this.perform('mine', { clicks_per_second: clicks_per_second }); } }); TradeWars.miningLoop(); });
我也在正在加载的页面的HTML中定义了内联:
<script> $(document).ready(function() { setTimeout(function() { TradeWars.planet_id = 21; }, 5000); }); </script>
当这最终确实触发时,我得到一个“ReferenceError:TradeWars未定义”javascript错误。
我在rails中启用了资产管道,并通过命令行预编译资产:
bin/rails assets:precompile
我也遇到了JavaScript错误(在本地开发和生产方面):
Uncaught Error: Bootstrap tooltips require Tether (http://tether.io/)
但我不认为那是它,加上我不想安装Tether。
任何帮助将非常感激。
与另一位开发人员合作,我们尝试了各种修复。
以下是我们首先要做的一些事情,以确保Action Cable正常运行。
在production.rb:
config.action_cable.url = "ws://tradewars.io/cable"
在应用程序布局(application.html.erb)中,添加了:
<%= action_cable_meta_tag %>
然后,最终解决问题(我们相信)解决了系绳错误!
Uncaught Error: Bootstrap tooltips require Tether (http://tether.io/)
通过将tether-rails gem添加到Gemfile:
gem 'tether-rails'
然后按照此处列出的步骤操作: https : //github.com/jakegavin/tether-rails
“Uncaught Error”(tether)消失了,jQuery神奇地开始启动文档就绪事件!
天哪,我喜欢这个职业。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.