Rails 5 - JS在生产环境中不工作(仅) [英] Rails 5 - JS not working in production environment (only)

查看:119
本文介绍了Rails 5 - JS在生产环境中不工作(仅)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我的应用程序/ assets / javascripts文件夹中有一系列js文件的rails 5应用程序。这些文件中的js在开发环境中工作得很好,但是当我在heroku上发布时,它停止工作。



从chrome检查器可以看到,应用程序从网络选项卡可见的.js文件显示我的应用程序中的js文件的内容。这只是在生产中似乎没有做任何事情。没有控制台错误出现在chrome控制台中。



这篇文章有一系列的事情可以尝试。我已经尝试了所有这些。我甚至创建了一个全新的应用程序,并一次添加了js文件,以查看是否存在需要以其他顺序执行某些操作(主要是猜测)的临界点 - 但这无法解决问题。



我可以从

 #不要这样做如果预编译的资产不存在,则回退到资产管道
config.assets.compile = false

然后尝试:

  rake assets:clean assets:precompile 

我试过了这些建议,但仍然找不到问题。



如果有以我写js的方式出错,我希望能够在Chrome的开发和生产环境控制台检查器中公开。在任一环境中都不会显示错误。

如果生产环境读取js文件时出现问题,我很困惑我为什么可以在文件中搜索js文件中使用的单词我可以从网络选项卡打开并查看正确的文本。



我该如何解决这个问题?



我的相关文件是:

app / assets / javascripts / application.js

  // =需要jquery 
// =需要jquery_ujs
// =需要turbolinks
// =需要jquery-fileupload / vendor / jquery.ui.widget
// =需要jquery-fileupload / jquery.iframe-transport
// =需要jquery-fileupload / jquery.fileupload
// =需要bootstrap-sprockets
// =需要时刻
//需要bootstrap-datetimepicker
//需要选择器
//需要下划线
//需要gmaps / google
// =需要markerclusterer
// =需要cocoon
// =需要电缆
// = require_tree。

config / initializers / assets.rb

 #修改此文件时一定要重新启动服务器。 
#您的资产版本,如果您想要过期所有资产,请更改此值。
Rails.application.config.assets.version ='1.0'

#将其他资产添加到资产加载路径
#Rails.application.config.assets.paths< < Emoji.images_path

#预编译其他资产。
#application.js,application.css以及app / assets文件夹中的所有非JS / CSS都已添加。
#Rails.application.config.assets.precompile + =%w(search.js)

config / production.rb

 #如果预编译的资源丢失,请不要回退到资产管道。 
config.assets.compile = false

相关文件在生产中不起作用环境保存在app / assets / javscripts / stance / commercial.js中(所以它应该通过// = require_tree在application.js中标识。)

该文件具有:

  jQuery(document).ready(function(){
jQuery('[name =organization [commercial_attributes] [standard_financial_split]]')。on('click',function(){
if(jQuery(this).val()=='true'){
jQuery('#commercial_standard_financial_split_content') .removeClass('hidden');
} else {
jQuery('#commercial_standard_financial_split_content')。removeClass('hidden')。addClass('hidden');
}
});

));

任何人都可以帮助进程让js在生产环境中工作?



我没有建议要尝试的东西他不是那种教程,编码学校(我是代码学校的一名学生,走上铁轨,udemy并拥有一百万本书),但找不到解决这个问题的参考点。对于我来说,解决方案是从我的application.html.erb的标题中删除javascript include标记,并将其放在下面。

身体。这解决了我在我的问题中引用的js文件的问题。



然而,现在这已经打破了我的谷歌地图JavaScript,所以我不确定这是实际的一个很好的解决方案。


I am struggling.

I have rails 5 app with a series of js files in my app/assets/javascripts folder. The js in those files works just fine in the development environment, but when I publish on heroku, it stops working.

I can see from the chrome inspector, that the application.js file that is visible from the network tab shows the content of the js files in my app. That js just doesnt seem to do anything in production. No console errors appear from the chrome console inpsector.

I can see from this post that there are a series of things to try. I've tried all of them. I've even made a completely new app and added the js files on at a time to see if there is a tipping point where something is required in a different order to something else (mostly guessing) - but that does nothing to fix the problem.

I can see from this article that one suggestion is to:

# Don't fallback to assets pipeline if a precompiled asset is missed
config.assets.compile = false

and then try:

rake assets:clean assets:precompile

I've tried each of these suggestions and still can't find the problem.

If there were an error in the way I've written the js, I would expect that to be exposed in both my development and production environment console inspectors in chrome. There are no errors showing in either environment.

If there were a problem with the production environment reading the js file, I'm confused about why I can word search for the words used in the js file in the file I can open from the network tab and see the correct text.

How do I go about solving this problem?

My relevant files are:

app/assets/javascripts/application.js

//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require jquery-fileupload/vendor/jquery.ui.widget
//= require jquery-fileupload/jquery.iframe-transport
//= require jquery-fileupload/jquery.fileupload
//= require bootstrap-sprockets
//= require moment
//= require bootstrap-datetimepicker
//= require pickers
// require underscore
// require gmaps/google
//= require markerclusterer
//= require cocoon
//= require cable
//= require_tree .

config/initializers/assets.rb

# Be sure to restart your server when you modify this file.
# Version of your assets, change this if you want to expire all your assets.
Rails.application.config.assets.version = '1.0'

# Add additional assets to the asset load path
# Rails.application.config.assets.paths << Emoji.images_path

# Precompile additional assets.
# application.js, application.css, and all non-JS/CSS in app/assets folder are already added.
# Rails.application.config.assets.precompile += %w( search.js )

config/production.rb

  # Do not fallback to assets pipeline if a precompiled asset is missed.
  config.assets.compile = false

The relevant file that is not working in the production environment is saved in app/assets/javscripts/stance/commercial.js (so it should be identified in application.js by //= require_tree.

That file has:

jQuery(document).ready(function() {
   jQuery('[name="organisation[commercial_attributes][standard_financial_split]"]').on('click', function() {
      if (jQuery(this).val() == 'true' ) {
          jQuery('#commercial_standard_financial_split_content').removeClass('hidden');
      } else {
          jQuery('#commercial_standard_financial_split_content').removeClass('hidden').addClass('hidden');
      }
   });

});

Can anyone help with a process for getting js to work in the production environment?

I am out of suggestions for things to try and this isn't the sort of thing that the tutorials, code schools (I am a student in code school, go rails, udemy and have a million books), but can't find an reference point from which to start in solving this problem.

解决方案

For me, the solution was to remove the javascript include tag from the header of my application.html.erb to beneath the body. That fixed the problem with the js file I referenced in my question.

However, this has now broken my google maps javascript, so I'm not sure that this is actually a good solution.

这篇关于Rails 5 - JS在生产环境中不工作(仅)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆