Rails - Understanding application.js and application.css

前端 未结 4 1082
囚心锁ツ
囚心锁ツ 2021-02-14 01:44

New to rails. Just trying to understand these two files in the \\assests directory.

For example, the application.js file has lines such as:

//= require j         


        
4条回答
  •  难免孤独
    2021-02-14 02:17

    the application.css and application.js are not regular css and js files (they could be, but they serve a different purpose

    both are manifest files which tell the asset pipeline along with sprockets for js

    so, as correctly pointed out by Michael Durrant's answer, http://guides.rubyonrails.org/asset_pipeline.html#manifest-files-and-directives will be the correct place

    but according to your other questions it seems you are missing a pretty critical piece of the puzzle

    Rails mainly works with gems. Gems are pieces of ruby code which you can tell to add to your rails app via bundler

    when you add such a gem like the bootstrap gem, it gets installed (by default in the gems library where you have ruby installed - something like Ruby193\lib\ruby\gems\1.9.1\gems)

    If you go there and look for the bootstrap gem you will find the css and js files that are included in the app, and also the jquery and jquery_ujs that you include in the manifest file

    since the gems get installed alongside rails, rails don't mind where the files are (as long as it knows where they are).

    So the manifest file tells rails "Hey, include these file for me, in this specific order" That is why you can include files that you wrote which are in the assets folder and files are included in a gem

    If you don't include the files in the manifest but still install the gem that is equivalent to writing a css or js file, placing it in some folder and not telling rails that it exists. When you do tell rails where it is, via the manifest file, it will include it in the asset compilation process and you can access it regularly.

    Alternativly, you don't have to use the asset pipeline for assets

    you can include css and js file with a regular

     
    

    and just host your files somewhere and point it to the files, but the asset pipeline has many advantages and its really makes your life easier when you get to know it

提交回复
热议问题