简体   繁体   English

jQuery文档准备好不在带有ActionCable的Rails 5应用程序中触发

[英]jQuery document ready not firing in a Rails 5 App with ActionCable

I've just deployed a new rails 5 app to production, but I cannot get the document ready function to fire. 我刚刚在生产中部署了一个新的rails 5应用程序,但我无法启动文档就绪功能。 The application, including ActionCable connection and rails console output in the document ready statement fire locally in Chrome, Firefox, etc. 应用程序,包括文档就绪语句中的ActionCable连接和rails控制台输出在Chrome,Firefox等本地激活。

Contents of my app/assets/javascripts/application.js: 我的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(); }); 

I also have this defined inline in the HTML of the page being loaded: 我也在正在加载的页面的HTML中定义了内联:

 <script> $(document).ready(function() { setTimeout(function() { TradeWars.planet_id = 21; }, 5000); }); </script> 

When this eventually does fire, I get a "ReferenceError: TradeWars is not defined" javascript error. 当这最终确实触发时,我得到一个“ReferenceError:TradeWars未定义”javascript错误。

I've enabled the asset pipeline in rails and am precompiling the assets via the command-line with: 我在rails中启用了资产管道,并通过命令行预编译资产:

bin/rails assets:precompile

I'm also getting the JavaScript error (on both local development & production): 我也遇到了JavaScript错误(在本地开发和生产方面):

Uncaught Error: Bootstrap tooltips require Tether (http://tether.io/)

But I don't think that would be it, plus I don't want to have to install Tether. 但我不认为那是它,加上我不想安装Tether。

Any help would be much appreciated. 任何帮助将非常感激。

Working with another developer, we tried various fixes. 与另一位开发人员合作,我们尝试了各种修复。

Here's a few things we did first to make sure Action Cable was working appropriately. 以下是我们首先要做的一些事情,以确保Action Cable正常运行。

In production.rb : 在production.rb:

config.action_cable.url = "ws://tradewars.io/cable"

In the application layout (application.html.erb), added: 在应用程序布局(application.html.erb)中,添加了:

<%= action_cable_meta_tag %>

Then, ultimately what solved the issue (we believe) was fixing the Tether error! 然后,最终解决问题(我们相信)解决了系绳错误!

Uncaught Error: Bootstrap tooltips require Tether (http://tether.io/)

By adding the tether-rails gem to the Gemfile: 通过将tether-rails gem添加到Gemfile:

gem 'tether-rails'

Then following the steps outlined here: https://github.com/jakegavin/tether-rails 然后按照此处列出的步骤操作: https//github.com/jakegavin/tether-rails

The "Uncaught Error" (tether) went away, and jQuery magically started firing the document ready event! “Uncaught Error”(tether)消失了,jQuery神奇地开始启动文档就绪事件!

Gosh I love this profession. 天哪,我喜欢这个职业。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM