How to install local fonts for rails 5.2

Viewed 3568

I am trying to add a font to my Rails 5.1 project, however, it seems the project can't find the font. Here is the directory for the fonts:

app
├── assets
│   ├── config
│   │   └── manifest.js
│   ├── fonts
│   │   ├── Open-Sans.eot
│   │   ├── Open-Sans.svg
│   │   ├── Open-Sans.ttf
│   │   └── Open-Sans.woff

In my application.rb I do added the fonts path:

config.assets.paths << Rails.root.join('app', 'assets', 'fonts')
config.assets.precompile << /\.(?:svg|eot|woff|ttf)$/

Here is my base.scss file:

@font-face {
  font-family: "Open-Sans";
  src: asset-url('Open-Sans.ttf') format('truetype');
}

body {
  font-family: "Open-Sans", Helvetica, Arial, sans-serif;
  // font-family: $body-font-family;
  // background-color: #2f4050;
  background-color: #222;
  font-size: 15px;
  color: $text-color;
  overflow-x: hidden;

}

I am not sure what is wrong... any suggestions?

1 Answers

I think you may have to modify the base.scss file

@font-face {
  font-family: "Open-Sans";
  src: url(asset-path('Open-Sans.ttf')) format("truetype");
}

body {
  font-family: "Open-Sans", Helvetica, Arial, sans-serif;
  // font-family: $body-font-family;
  // background-color: #2f4050;
  background-color: #222;
  font-size: 15px;
  color: $text-color;
  overflow-x: hidden;

}

After this change, You have to restart your web server.

Related