Rails 4 + bootstrap set up assets

前端 未结 8 1516
春和景丽
春和景丽 2021-02-04 06:53

I am trying to setup bootstrap on Rails4 using bootstrap-sass and I am getting this famous error:

Sprockets::FileNotFound - couldn         


        
8条回答
  •  生来不讨喜
    2021-02-04 07:30

    On one of my projects (Rails 4.1) I had to include the bootstrap directly (not sass). Maybe it will give a hint on making the saas version work. So below are steps to include the bootstrap directly:

    1. Download and and extract the bootstrap to Rails.root/vendor/assets/bootstrap
    2. Create file Rails.root/vendor/assets/javascripts/bootstrap.js file with contents like this:

      //= require ../bootstrap/js/bootstrap.js

    3. Now the most important part to make icons work. The font file urls have to be overridden for the Glyphicons Halflings font. Also asset_path helper has to be used. So create file Rails.root/vendor/assets/stylesheets/bootstrap.css.erb file with contents like this.

    /*
    =require ../bootstrap/css/bootstrap.css
    */
    
    @font-face {
        font-family: 'Glyphicons Halflings';
    
        src: url("<%= asset_path 'glyphicons-halflings-regular.eot' %>");
        src: url("<%= asset_path 'glyphicons-halflings-regular.eot?#iefix' %>") format('embedded-opentype'), url("<%= asset_path 'glyphicons-halflings-regular.woff2' %>") format('woff2'), url("<%= asset_url 'glyphicons-halflings-regular.woff' %>") format('woff'), url("<%= asset_path 'glyphicons-halflings-regular.ttf' %>") format('truetype'), url("<%= asset_path 'glyphicons-halflings-regular.svg#glyphicons_halflingsregular' %>") format('svg');
    }
    
    1. Now require bootstrap in the application.js and application.css

    application.js

    //= require bootstrap
    

    application.css

     *= require bootstrap
    
    1. And finally let assets pipeline be aware of the fonts path and additional extensions to precompile. In the application.rb add:
    config.assets.paths << Rails.root.join("vendor", "assets", "bootstrap", "fonts")
    config.assets.precompile += %w( *.eot *.svg *.ttf *.woff *.woff2 )
    

    After that RAILS_ENV=production rake assets:precompile should show that is has recognized font files and copied them to the public assets folder.

    Then to test if it works in production enable serving static assets (in production.rb: config.serve_static_assets = true) and RAILS_ENV=production rails s

提交回复
热议问题