Rails 4.0 中的 @font-face,未找到自定义字体的 Sass URL [英] @font-face in Rails 4.0 with Sass URL not found for custom fonts

查看:27
本文介绍了Rails 4.0 中的 @font-face,未找到自定义字体的 Sass URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 Rails 项目中,我尝试使用自定义字体.有很多与这个问题相关的答案,比如这个没有帮助的答案,我编辑了 development.rb:

In my Rails project I am trying to use custom fonts. There are lots of answers related to this question, like this answer which didn't help, I edited 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 )

仍然显示 没有路由匹配 [GET] "/assets/chalkduster-webfont.woff"

我给我设置了 url 像:

@font-face {
   font-family: 'chalkdusterregular';
    src: url('chalkduster-webfont.eot');
    src:url('chalkduster-webfont.svg#chalkdusterregular') format('svg'), 
    url('chalkduster-webfont.eot?#iefix') format('embedded-opentype'),
         url('chalkduster-webfont.woff') format('woff'),
         url('chalkduster-webfont.ttf') format('truetype');
    font-weight: normal;
    font-style: normal;

}

我在源代码中尝试了 font_path('') 并且 font-url() 它从来没有工作.:(

I tried font_path('') within source and also font-url() it never works. :(

推荐答案

尝试 asset-url().对我有用.

@font-face {
   font-family: 'chalkdusterregular';
    src: asset-url('chalkduster-webfont.eot');
    src: asset-url('chalkduster-webfont.svg#chalkdusterregular') format('svg'), 
         asset-url('chalkduster-webfont.eot?#iefix') format('embedded-opentype'),
         asset-url('chalkduster-webfont.woff') format('woff'),
         asset-url('chalkduster-webfont.ttf') format('truetype');
    font-weight: normal;
    font-style: normal;
}

此外,我只在 config/environments/production.rb 中添加字体路径并预编译其他资产

Also, I only add the fonts path and precompile additional assets in config/environments/production.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 )

不需要添加到 config/environments/development.rb 因为 asset-url 有点神奇.

Doesn't need to be added to config/environments/development.rb as asset-url works a little magic.

这篇关于Rails 4.0 中的 @font-face,未找到自定义字体的 Sass URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆