Using fonts with Rails asset pipeline

前端 未结 12 1627
慢半拍i
慢半拍i 2020-11-22 14:52

I have some fonts being configured in my Scss file like so:

@font-face {
  font-family: \'Icomoon\';
  src: asset-url(\'icoMoon.eot?#iefix\', font) format(\'         


        
相关标签:
12条回答
  • 2020-11-22 15:04

    I had a similar issue when I upgraded my Rails 3 app to Rails 4 recently. My fonts were not working properly as in the Rails 4+, we are only allowed to keep the fonts under app/assets/fonts directory. But my Rails 3 app had a different font organization. So I had to configure the app so that it still works with Rails 4+ having my fonts in a different place other than app/assets/fonts. I have tried several solutions but after I found non-stupid-digest-assets gem, it just made it so easy.

    Add this gem by adding the following line to your Gemfile:

    gem 'non-stupid-digest-assets'
    

    Then run:

    bundle install
    

    And finally add the following line in your config/initializers/non_digest_assets.rb file:

    NonStupidDigestAssets.whitelist = [ /\.(?:svg|eot|woff|ttf)$/ ]
    

    That's it. This solved my problem nicely. Hope this helps someone who have encountered similar problem like me.

    0 讨论(0)
  • 2020-11-22 15:06

    If you have a file called scaffolds.css.scss, then there's a chance that's overriding all the custom things you're doing in the other files. I commented out that file and suddenly everything worked. If there isn't anything important in that file, you might as well just delete it!

    0 讨论(0)
  • 2020-11-22 15:06

    just place your fonts inside app/assets/fonts folder and set the autoload path when app start using writing the code in application.rb

    config.assets.paths << Rails.root.join("app", "assets", "fonts") and

    then use the following code in css.

    @font-face {

     font-family: 'icomoon';
     src: asset-url('icomoon.eot');
     src: asset-url('icomoon.eot') format('embedded-opentype'),
          asset-url('icomoon.woff') format('woff'),
          asset-url('icomoon.ttf') format('truetype'),
          asset-url('icomoon.svg') format('svg');
     font-weight: normal;
     font-style: normal;
    

    }

    Give it a try.

    Thanks

    0 讨论(0)
  • 2020-11-22 15:12

    In my case the original question was using asset-url without results instead of plain url css property. Using asset-url ended up working for me in Heroku. Plus setting the fonts in /assets/fonts folder and calling asset-url('font.eot') without adding any subfolder or any other configuration to it.

    0 讨论(0)
  • 2020-11-22 15:14

    You need to use font-url in your @font-face block, not url

    @font-face {
    font-family: 'Inconsolata';
    src:font-url('Inconsolata-Regular.ttf') format('truetype');
    font-weight: normal;
    font-style: normal;
    }
    

    as well as this line in application.rb, as you mentioned (for fonts in app/assets/fonts

    config.assets.paths << Rails.root.join("app", "assets", "fonts")
    
    0 讨论(0)
  • 2020-11-22 15:15

    I was having this problem on Rails 4.2 (with ruby 2.2.3) and had to edit the font-awesome _paths.scss partial to remove references to $fa-font-path and removing a leading forward slash. The following was broken:

    @font-face {
      font-family: 'FontAwesome';
      src: font-url('#{$fa-font-path}/fontawesome-webfont.eot?v=#{$fa-version}');
      src: font-url('#{$fa-font-path}/fontawesome-webfont.eot?#iefix&v=#{$fa-version}') format('embedded-opentype'),
        font-url('#{$fa-font-path}/fontawesome-webfont.woff2?v=#{$fa-version}') format('woff2'),
        font-url('#{$fa-font-path}/fontawesome-webfont.woff?v=#{$fa-version}') format('woff'),
        font-url('#{$fa-font-path}/fontawesome-webfont.ttf?v=#{$fa-version}') format('truetype'),
        font-url('#{$fa-font-path}/fontawesome-webfont.svg?v=#{$fa-version}#fontawesomeregular') format('svg');
      font-weight: normal;
      font-style: normal;
    }
    

    And the following works:

    @font-face {
      font-family: 'FontAwesome';
      src: font-url('fontawesome-webfont.eot?v=#{$fa-version}');
      src: font-url('fontawesome-webfont.eot?#iefix&v=#{$fa-version}') format('embedded-opentype'),
        font-url('fontawesome-webfont.woff2?v=#{$fa-version}') format('woff2'),
        font-url('fontawesome-webfont.woff?v=#{$fa-version}') format('woff'),
        font-url('fontawesome-webfont.ttf?v=#{$fa-version}') format('truetype'),
        font-url('fontawesome-webfont.svg?v=#{$fa-version}#fontawesomeregular') format('svg');
      font-weight: normal;
      font-style: normal;
    }
    

    An alternative would be to simply remove the forward slash following the interpolated $fa-font-path and then define $fa-font-path as an empty string or subdirectory with trailing forward slash (as needed).

    Remember to recompile assets and restart your server as needed. For example, on a passenger setup:

    prompt> rake assets:clean; rake assets:clobber
    prompt> RAILS_ENV=production RAILS_GROUPS=assets rake assets:precompile
    prompt> service passenger restart
    

    Then reload your browser.

    0 讨论(0)
提交回复
热议问题