My application.js won't work and application.css won't update in Rails

て烟熏妆下的殇ゞ 提交于 2021-02-10 17:56:45

问题


I configured my application to work on Heroku, but I'm having a problem. In development mode, when I change a part of the CSS and refresh the browser, my CSS doesn't load.

I checked the logs, and the application.css doesn't load. I need it to load every time in development mode. Also, my application.js stopped working and I dont know why.

development.rb:

  # Do not compress assets
  config.assets.compress = false
  config.assets.precompile += %w( login.css )

  # Expands the lines which load the assets
  config.assets.debug = true

enviroment.rb:

  config.active_record.whitelist_attributes = true

   # Enable the asset pipeline
   config.assets.enabled = true

   # Version of your assets, change this if you want to expire all your assets
   config.assets.version = '1.0'

   config.assets.initialize_on_precompile = true

I compile the assets manually using $ rake assets:precompile RAILS_ENV=development and it works except for the application.js code:

//= require jquery
//= require jquery_ujs
//= require_tree .
//= require jquery.reveal
$(document).ajaxStart(function() {
    $( "#load_img" ).animate({
            height:"toggle",
      opacity:"toggle"
        });
});

$(document).ajaxSuccess(function() {
    $( "#load_img" ).fadeOut('fast');
});

$(document).ready(function() {
  setTimeout(
    function() {
      $("#flash_messages").fadeOut(3000)
    }, 
    2500);
});

Everything worked before deploying to Heroku.


回答1:


You don't require the application.js file itself. Use require_self like so:

//= require jquery
...
//= require_self

$(function() {});

Also pretty sure you're supposed to have a line break between the end of the includes and the start of the code in the application.js file itself.

Also this line:

config.assets.precompile += %w( login.css )

should probably be in application.rb and not just development.rb, although if you are manually precompiling assets in development mode before deploying to Heroku it shouldn't matter.




回答2:


In case you want to see changes in development, you should not precompile your assets . To revert precompilation , try rake assets:clean . Also , it is not a good style to have js code in the manifest file application.js. Create a new file in app/assets/javascripts/ and move the code there.




回答3:


Precompiling your assets in development means that you're going to need to keep precompiling them to see any changes you make.

When you precompile assets, this will create a static css file in public/assets, and your web server will always serve static assets (eg. those in public/assets) before looking in the asset pipeline for new files.

Clean the precompiled files by either manually emptying public/assets or calling rake assets:clean.

When you deploy to Heroku, it will automatically compile the assets for you on deploy.



来源:https://stackoverflow.com/questions/14905117/my-application-js-wont-work-and-application-css-wont-update-in-rails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!