简体   繁体   English

使用Rails 3.1 Asset Pipeline在web中存储webfonts

[英]Storing webfonts in a gem using the Rails 3.1 Asset Pipeline

I'm trying to use the Rails 3.1 Asset Pipeline to store some fonts that I'm using across multiple applications. 我正在尝试使用Rails 3.1 Asset Pipeline来存储我在多个应用程序中使用的一些字体。 I've tried just about every combination of storage location but I can't seem to get the pipeline to actually pick up my font files. 我已经尝试了几乎存储位置的每个组合,但我似乎无法让管道实际上拿起我的字体文件。 They will show up in public/assets when I run rake assets:precompile but they aren't available from any asset_path helpers and I haven't been able to figure out why. 当我运行rake assets:precompile public/assets时,它们将显示在public/assets rake assets:precompile但是它们不能从任何asset_path帮助程序中获得,并且我无法弄清楚原因。

Example from fonts.css.erb : fonts.css.erb示例:

@font-face {
    font-family: 'MuseoSans';
    src: url('<%= asset_path('museosans_500_italic_webfont.eot') %>');
    src: url('<%= asset_path('museosans_500_italic_webfont.eot?#iefix') %>') format('eot'),
         url('<%= asset_path('museosans_500_italic_webfont.woff') %>') format('woff'),
         url('<%= asset_path('museosans_500_italic_webfont.ttf') %>') format('truetype'),
         url('<%= asset_path('museosans_500_italic_webfont.svg#webfontcWw5DXpH') %>') format('svg');
    font-weight: normal;
    font-style: italic;

 }

Output of ls app/assets/images (stuck it in images since assets/fonts wasn't working, but had the same lack of working both times): ls app/assets/images输出(由于assets/fonts无效,但两次都缺乏工作,因此将其粘贴在images ):

museosans_100_italic_webfont.eot  museosans_500_webfont.eot
museosans_100_italic_webfont.svg  museosans_500_webfont.svg
museosans_100_italic_webfont.ttf  museosans_500_webfont.ttf
museosans_100_italic_webfont.woff museosans_500_webfont.woff
museosans_100_webfont.eot         museosans_700_italic_webfont.eot
museosans_100_webfont.svg         museosans_700_italic_webfont.svg
museosans_100_webfont.ttf         museosans_700_italic_webfont.ttf
museosans_100_webfont.woff        museosans_700_italic_webfont.woff
museosans_300_italic_webfont.eot  museosans_700_webfont.eot
museosans_300_italic_webfont.svg  museosans_700_webfont.svg
museosans_300_italic_webfont.ttf  museosans_700_webfont.ttf
museosans_300_italic_webfont.woff museosans_700_webfont.woff
museosans_300_webfont.eot         museosans_900_italic_webfont.eot
museosans_300_webfont.svg         museosans_900_italic_webfont.svg
museosans_300_webfont.ttf         museosans_900_italic_webfont.ttf
museosans_300_webfont.woff        museosans_900_italic_webfont.woff
museosans_500_italic_webfont.eot  museosans_900_webfont.eot
museosans_500_italic_webfont.svg  museosans_900_webfont.svg
museosans_500_italic_webfont.ttf  museosans_900_webfont.ttf
museosans_500_italic_webfont.woff museosans_900_webfont.woff

I've tried accessing from: 我试过访问:

  • /assets/museosans_500_italic_webfont.svg
  • /assets/images/museosans_500_italic_webfont.svg
  • /images/museosans_500_italic_webfont.svg

It's not picking it up anywhere. 它不会在任何地方捡到它。 Any thoughts? 有什么想法吗?

Are you explicitly declaring font files should be precompiled? 您是否明确声明应该预编译字体文件?

config.assets.precompile += %w( .js .css *.css.scss .svg .eot .woff .ttf) config.assets.precompile + =%w(.js .css * .css.scss .svg .eot .woff .ttf)

Is this a problem in development, staging, production, all of the above? 这是开发,升级,生产,上述所有问题吗?

Is the stylesheet with the @font-face rule loading? 是否加载了@ font-face规则的样式表?

You should tell asset_path you're 'assetising' a font, that will fix your directory problem: 你应该告诉asset_path你'资产'一种字体,这将修复你的目录问题:

url('<%= asset_path('museosans_500_italic_webfont.eot', font) %>');

If that doesn't work, what is the browser output from application.css (I assume that includes fonts.css)? 如果这不起作用,application.css的浏览器输出是什么(我假设包括fonts.css)?

The docs say: 文档说:

Asset Helpers . 资产助手 When using the asset pipeline, paths to assets must be rewritten. 使用资产管道时,必须重写资产路径。 When referencing assets use the following asset helpers (underscored in Ruby, hyphenated in Sass): 引用资产时使用以下资产助手(在Ruby中加以强调,在Sass中用连字符表示):

◦   asset_path($relative-asset-path, $asset-class) - Returns a string to the asset. For example: asset-path("rails.png", image) becomes "/assets/rails.png"
◦   asset_url($relative-asset-path, $asset-class) - Returns url reference to the asset.

For example: asset-url("rails.png", image) becomes url(/assets/rails.png) 例如:asset-url(“rails.png”,image)变为url(/assets/rails.png)

As a convenience , for each of the following asset classes there are corresponding -path and -url helpers: image, font, video, audio, javascript, stylesheet. 为方便起见 ,对于以下每个资产类,都有相应的-path和-url助手:图像,字体,视频,音频,javascript,样式表。 For example: image-url("rails.png") becomes url(/assets/rails.png) and image-path("rails.png") becomes "/assets/rails.png". 例如:image-url(“rails.png”)变为url(/assets/rails.png),image-path(“rails.png”)变为“/assets/rails.png”。

Example: 例:

@font-face
  font-family: HelveticaInseratCom
  src: font-url('HelveticaInseratCom.ttf')

But, i could not do it like that for my rails3.1 app. 但是,我不能像我的rails3.1应用那样做。 I had to put the fonts directly in the public folder: 我不得不将字体直接放在公共文件夹中:

/public/HelveticaInseratCom.ttf

And in the css.scss.erb file i used: 在我使用的css.scss.erb文件中:

@font-face
  font-family: HelveticaInseratCom
  src: url('/HelveticaInseratCom.ttf')    

This then worked and when running rake assets:precompile worked without throwing this error: 然后这运行并且在运行rake assets:precompile工作而不会抛出此错误:

rake aborted!
HelveticaInseratCom.ttf isn't precompiled

If you stick the fonts in your assets/images directory, and then use rake assets:precompile , rails automatically compiles everything in your images directory. 如果您将字体粘贴在assets / images目录中,然后使用rake assets:precompile ,rails会自动编译images目录中的所有内容。

so all you have to do is make the correct call in your css: 所以你要做的就是在你的CSS中拨打正确的电话:

If *.css.scss : 如果*.css.scss

font-url('museosans_500_italic_webfont.eot') format('eot')

Note: you can use font-url (reference rails guides ) 注意:您可以使用font-url (参考导轨指南

Gotcha #1: add your css files you want to serve (only the ones you actually call in stylesheet_link_tag) config.assets.precompile += %w( application.css.scss application.js.coffee ect.css.sass) 问题#1:添加你想要服务的css文件(只有你在stylesheet_link_tag中实际调用的文件) config.assets.precompile += %w( application.css.scss application.js.coffee ect.css.sass)

Gotcha #2: you need to specify digest to get it to work: 问题#2:您需要指定摘要才能使其工作:

# Generate digests for assets URLs
config.assets.digest = true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM