简体   繁体   中英

Rails 4 - .js files not running in production environment - Heroku

I'm having trouble loading my .js files in production on Heroku.

I know the files are being picked up from the asset pipeline as they are working in development just fine but I am unable to get the .js working in production.

I have created /assets/javascripts/notification.js containing:

$(document).ready(function() {
  $("span").click(function() {
    $(".notice").delay(200).fadeOut(2000, function() {
    });
  });
});

My /assets/javascripts/application.js file contains:

//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require_tree .

in development, this works great.

When I deploy to Heroku, I can see that the code is displayed within a single application.js file, except the document ready function is stripped out, see below production .js file:

<script src="/assets/application-b9a733c3cc0929ae852e45a6c3a2a5f6.js" data-turbolinks-track="true"></script>

My config/production.rb contains:

config.serve_static_files = ENV['RAILS_SERVE_STATIC_FILES'].present?
config.assets.compile = true

If you install the rails_12factor gem as Heroku Recommends it will automatically run rake assets:precompile on deploy (through a gem dependency named rails_serve_static_assets ).

It may skip this step if it sees a public/assets/manifest.* file or a /public/assets directory, which tells it you've already compiled the assets, and it doesn't need to be done again.

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