这是我所做的:
首先,我在目录中创建了一个fonts
文件夹app/assets
。
然后我配置了资产管道以识别这个新文件夹。
config/environments/development.rb
:
# Add the fonts path
config.assets.paths << Rails.root.join('app', 'assets', 'fonts')
# Precompile additional assets
config.assets.precompile += %w( .svg .eot .woff .ttf .otf )
接下来,我在中配置 mime 类型config/initializers/mime_types.rb
:
# Be sure to restart your server when you modify this file.
# Here are some example that came with the default Rails project.
# Add new mime types for use in respond_to blocks:
# Mime::Type.register "text/richtext", :rtf
# Mime::Type.register_alias "text/html", :iphone
Rack::Mime::MIME_TYPES['.otf'] = 'application/x-font-opentype'
最后,我在 SCSS 中引用了字体:
@font-face {
font-family: 'ArnoProDisplay';
src: url('ArnoPro-Display.otf');
font-weight: normal;
font-style: normal;
}
我正在使用谷歌浏览器,控制台说:
Resource interpreted as Font but transferred with MIME type
application/vnd.oasis.opendocument.formula-template:
"http://localhost:3000/assets/ArnoPro-Display.otf".
我可以在“网络”选项卡中正确看到字体: