这是我所做的:
首先,在app/assets
目录下创建了一个fonts
文件夹。
然后,配置资产管道以识别这个新文件夹。
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 )
接下来,我在
config/initializers/mime_types.rb
中配置mime类型:# 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;
}
我正在使用Google Chrome浏览器,控制台显示如下信息:
Resource interpreted as Font but transferred with MIME type
application/vnd.oasis.opendocument.formula-template:
"http://localhost:3000/assets/ArnoPro-Display.otf".
我可以在网络选项卡中正确地看到字体:
.htaccess
文件,并添加AddType application/vnd.oasis.opendocument.formula-template .otf
。 - rails_idvnd.oasis.opendocument.formula-template
吗?我正试图防止它具有该MIME类型,而不是鼓励它。 - sergsergsrc: url(/assets/ArnoPro-Display.otf);
)无法正常工作,因为字体的实际 URL 变成了assets/assets/ArnoPro-Display.otf
。请记住,我已经在资产管道中注册了该文件夹。 - sergsergmime_type.rb
之后,您应该在重新启动之前清除缓存
。希望这可以帮助您。 - rails_id