Search code examples
cssruby-on-railsfontssasswebfonts

How do I use a custom font in Ruby on Rails?


I have

users.scss
@font-face {
  font-family: scriptina;
  src: asset-url('scriptina.ttf')
}

.script {
  font: scriptina, cursive; 
}

Which generates

@font-face {
  font-family: scriptina;
  src: url(/scriptina.ttf);
}
/* line 18, C:/Users/Chloe/workspace//app/assets/stylesheets/users.scss */
.script {
  font: scriptina, cursive;
}

But http://localhost:3000/scriptina.ttf generates

Routing Error
No route matches [GET] "/scriptina.ttf"

$ ls app/assets
config  images  javascripts  scriptina.ttf  stylesheets

Rails 5

Reference: http://guides.rubyonrails.org/asset_pipeline.html#coding-links-to-assets


Solution

  • One method (outlined here) would be to add a fonts folder to app/assets, and then add it to the folders that your app will look for assets in by editing config/application.rb:

    # config/application.rb
    config.assets.paths << Rails.root.join("app", "assets", "fonts")
    

    Then move scriptina.ttf into app/assets/fonts and your asset-url helper will then be referencing a valid path to your font.