简体   繁体   中英

Rails in production: JavaScript files not run

I try to run rails application (Rails 4.2.0) in production mode with these settings:

config/environment/production.rb

require 'uglifier'

Rails.application.configure do
  config.cache_classes = true
  config.eager_load = true
  config.consider_all_requests_local       = false
  config.action_controller.perform_caching = true
  config.assets.js_compressor = Uglifier.new(output: {ascii_only: true, quote_keys: true})
  config.serve_static_files = true
  config.assets.js_compressor = :uglifier
  config.assets.compile = false
  config.assets.digest = true
  config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' 
  config.log_level = :debug
  config.i18n.fallbacks = true
end

config/initializer/assets.rb

Rails.application.config.assets.version = '1.0'
Rails.application.config.assets.precompile += %w( ckeditor/* admin.*)

app/assets/application.js

//= require jquery
//= require jquery_ujs
//= require bootstrap-sprockets
//= require ckeditor/init
//= require select2
//= require init/select2
//= require lib/jquery.mCustomScrollbar.min
//= require lib/moment.min
//= require lib/daterangepicker
//= require init/daterangepicker
//= require lib/bootstrap-notify
//= require underscore
//= require gmaps/google
// lightgallery:
//= require lib/lightgallery/picturefill.min.js
//= require lib/lightgallery/lightgallery
//= require lib/jquery.mousewheel.min.js
//= require jquery.sortable
//= require photos
//= require_tree ./admin
//= require_tree .

app/assets/photos.js

// Loading photo for resources
(function($) {
  conslole.log('It is photos.js')
  // Check File API support
  if(window.File && window.FileList && window.FileReader){
    var filesInput = document.getElementById('upload_photos');
    //...
  }

}());

I expect to see in browser console message 'It is photos.js', but it's absent.

I explored compiled file

http://localhost:3000/assets/admin-a0144eae675b202d7e9a9fbca944636e6aa24fe7b0140b44ec68928303bc4717.js

and found this piece of code in browser sources:

(function($) {
  console.log('It is photos.js')
  // Check File API support
  if(window.File && window.FileList && window.FileReader){
    var filesInput = document.getElementById('upload_photos');
    //...
  }
}());

I can't understand why no message is shown in console.

But a lot of other js files and libs are working correct.

Try rake assets:precompile in your app directory then push the new compiled files in production. I ran into the same problem with asset files not loading properly in production and this command did the trick for me.

Hope it helps ;)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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