在Rails资产管道中使用字体 [英] Using fonts with Rails asset pipeline
问题描述
我在Scss文件中配置了一些字体,如下所示:
@font-face {
font-family: 'Icomoon';
src: asset-url('icoMoon.eot?#iefix', font) format('embedded-opentype'),
asset-url('icoMoon.woff', font) format('woff'),
asset-url('icoMoon.ttf', font) format('truetype'),
asset-url('icoMoon.svg#Icomoon', font) format('svg');
}
实际的字体文件存储在/app/assets/fonts/
中我已将config.assets.paths << Rails.root.join("app", "assets", "fonts")
添加到我的application.rb文件中
,编译的CSS源代码如下:
@font-face {
font-family: 'Icomoon';
src: url(/assets/icoMoon.eot?#iefix) format("embedded-opentype"), url(/assets/icoMoon.woff) format("woff"), url(/assets/icoMoon.ttf) format("truetype"), url(/assets/icoMoon.svg#Icomoon) format("svg");
}
但是当我运行该应用程序时,找不到字体文件.日志:
从2012-06-05 23:21:17 +0100开始为127.0.0.1获取GET"/assets/icoMoon.ttf" 投放的资产/icoMoon.ttf-找不到404(13毫秒)
为什么资产管道不将字体文件压缩为/assets?
有想法的人吗?
亲切的问候, 尼尔
其他信息:
在Rails控制台中检查资产路径和资产预编译时,我得到以下信息:
1.9.2p320 :001 > y Rails.application.config.assets.precompile
---
- !ruby/object:Proc {}
- !ruby/regexp /(?:\/|\\|\A)application\.(css|js)$/
- .svg
- .eot
- .woff
- .ttf
=> nil
1.9.2p320 :002 > y Rails.application.config.assets.paths
---
- /Users/neiltonge/code/neiltonge/app/assets/fonts
- /Users/neiltonge/code/neiltonge/app/assets/images
- /Users/neiltonge/code/neiltonge/app/assets/javascripts
- /Users/neiltonge/code/neiltonge/app/assets/stylesheets
- /Users/neiltonge/code/neiltonge/vendor/assets/images
- /Users/neiltonge/code/neiltonge/vendor/assets/javascripts
- /Users/neiltonge/code/neiltonge/vendor/assets/stylesheets
- /Users/neiltonge/.rvm/gems/ruby-1.9.2-p320@neiltonge/gems/jquery-rails-2.0.0/vendor/assets/javascripts
- /Users/neiltonge/.rvm/gems/ruby-1.9.2-p320@neiltonge/gems/coffee-rails-3.2.1/lib/assets/javascripts
- /Users/neiltonge/.rvm/gems/ruby-1.9.2-p320@neiltonge/gems/bourbon-1.3.0/app/assets/stylesheets
- !ruby/object:Pathname
path: /Users/neiltonge/code/neiltonge/app/assets/fonts
=> nil
-
如果您的Rails版本在
> 3.1.0
和< 4
之间,请将字体放在以下任何文件夹中:-
app/assets/fonts
-
lib/assets/fonts
-
vendor/assets/fonts
对于Rails版本
> 4
,您必须将字体放置在app/assets/fonts
文件夹中.注意:要在这些指定文件夹之外放置字体,请使用以下配置:
config.assets.precompile << /\.(?:svg|eot|woff|ttf)\z/
对于Rails版本
> 4.2
,推荐建议将此配置添加到config/initializers/assets.rb
.但是,您也可以将其添加到
config/application.rb
或config/production.rb
-
-
在CSS文件中声明字体:
@font-face { font-family: 'Icomoon'; src:url('icomoon.eot'); src:url('icomoon.eot?#iefix') format('embedded-opentype'), url('icomoon.svg#icomoon') format('svg'), url('icomoon.woff') format('woff'), url('icomoon.ttf') format('truetype'); font-weight: normal; font-style: normal; }
确保您的字体名称与声明的URL部分中的名称完全相同.大写字母和标点符号很重要.在这种情况下,字体的名称应为
icomoon
. -
如果您在Rails
> 3.1.0
中使用Sass或更少(您的CSS文件具有.scss
或.less
扩展名),则将字体声明中的url(...)
更改为font-url(...)
.>否则,您的CSS文件应具有扩展名
.css.erb
,字体声明应为url('<%= asset_path(...) %>')
.如果使用的是Rails
> 3.2.1
,则可以使用font_path(...)
代替asset_path(...)
.该帮助程序做的是完全相同的事情,但更清楚了. -
最后,像在
font-family
部分中声明的那样,在CSS中使用字体.如果声明为大写,则可以这样使用它:font-family: 'Icomoon';
I have some fonts being configured in my Scss file like so:
@font-face {
font-family: 'Icomoon';
src: asset-url('icoMoon.eot?#iefix', font) format('embedded-opentype'),
asset-url('icoMoon.woff', font) format('woff'),
asset-url('icoMoon.ttf', font) format('truetype'),
asset-url('icoMoon.svg#Icomoon', font) format('svg');
}
The actual font file are stored in /app/assets/fonts/
I have added config.assets.paths << Rails.root.join("app", "assets", "fonts")
to my application.rb file
and the compile CSS source is as follows:
@font-face {
font-family: 'Icomoon';
src: url(/assets/icoMoon.eot?#iefix) format("embedded-opentype"), url(/assets/icoMoon.woff) format("woff"), url(/assets/icoMoon.ttf) format("truetype"), url(/assets/icoMoon.svg#Icomoon) format("svg");
}
But when I run the app the font files are not being found. The logs:
Started GET "/assets/icoMoon.ttf" for 127.0.0.1 at 2012-06-05 23:21:17 +0100 Served asset /icoMoon.ttf - 404 Not Found (13ms)
Why isn't the asset pipeline flattening the font files down into just /assets?
Any ideas people?
Kind regards, Neil
Extra info:
When checking the rails console for assets paths and assetprecompile I get the following:
1.9.2p320 :001 > y Rails.application.config.assets.precompile
---
- !ruby/object:Proc {}
- !ruby/regexp /(?:\/|\\|\A)application\.(css|js)$/
- .svg
- .eot
- .woff
- .ttf
=> nil
1.9.2p320 :002 > y Rails.application.config.assets.paths
---
- /Users/neiltonge/code/neiltonge/app/assets/fonts
- /Users/neiltonge/code/neiltonge/app/assets/images
- /Users/neiltonge/code/neiltonge/app/assets/javascripts
- /Users/neiltonge/code/neiltonge/app/assets/stylesheets
- /Users/neiltonge/code/neiltonge/vendor/assets/images
- /Users/neiltonge/code/neiltonge/vendor/assets/javascripts
- /Users/neiltonge/code/neiltonge/vendor/assets/stylesheets
- /Users/neiltonge/.rvm/gems/ruby-1.9.2-p320@neiltonge/gems/jquery-rails-2.0.0/vendor/assets/javascripts
- /Users/neiltonge/.rvm/gems/ruby-1.9.2-p320@neiltonge/gems/coffee-rails-3.2.1/lib/assets/javascripts
- /Users/neiltonge/.rvm/gems/ruby-1.9.2-p320@neiltonge/gems/bourbon-1.3.0/app/assets/stylesheets
- !ruby/object:Pathname
path: /Users/neiltonge/code/neiltonge/app/assets/fonts
=> nil
If your Rails version is between
> 3.1.0
and< 4
, place your fonts in any of the these folders:app/assets/fonts
lib/assets/fonts
vendor/assets/fonts
For Rails versions
> 4
, you must place your fonts in theapp/assets/fonts
folder.Note: To place fonts outside of these designated folders, use the following configuration:
config.assets.precompile << /\.(?:svg|eot|woff|ttf)\z/
For Rails versions
> 4.2
, it is recommended to add this configuration toconfig/initializers/assets.rb
.However, you can also add it to either
config/application.rb
, or toconfig/production.rb
Declare your font in your CSS file:
@font-face { font-family: 'Icomoon'; src:url('icomoon.eot'); src:url('icomoon.eot?#iefix') format('embedded-opentype'), url('icomoon.svg#icomoon') format('svg'), url('icomoon.woff') format('woff'), url('icomoon.ttf') format('truetype'); font-weight: normal; font-style: normal; }
Make sure your font is named exactly the same as in the URL portion of the declaration. Capital letters and punctuation marks matter. In this case, the font should have the name
icomoon
.If you are using Sass or Less with Rails
> 3.1.0
(your CSS file has.scss
or.less
extension), then change theurl(...)
in the font declaration tofont-url(...)
.Otherwise, your CSS file should have the extension
.css.erb
, and the font declaration should beurl('<%= asset_path(...) %>')
.If you are using Rails
> 3.2.1
, you can usefont_path(...)
instead ofasset_path(...)
. This helper does exactly the same thing but it's more clear.Finally, use your font in your CSS like you declared it in the
font-family
part. If it was declared capitalized, you can use it like this:font-family: 'Icomoon';
这篇关于在Rails资产管道中使用字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!