热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

使用带有Rails资产管道的字体。-UsingfontswithRailsassetpipeline

IhavesomefontsbeingconfiguredinmyScssfilelikeso:我的Scss文件中配置了一些字体:@font-face{font-f

I have some fonts being configured in my Scss file like so:

我的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');
}

The actual font file are stored in /app/assets/fonts/

实际的字体文件存储在/app/资产/字体/中。

I have added config.assets.paths < to my application.rb file

我有添加config.assets。<

and the compile CSS source is as follows:

编译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");
}

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)

开始得到“/资产/ icoMoon。ttf“127.0.0.1 at 2012-06-05 23:21:17 +0100服务资产/icoMoon”。ttf - 404未找到(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:

在检查资产路径和assetprecompile的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

11 个解决方案

#1


602  

  1. If your Rails version is between > 3.1.0 and <4, place your fonts in any of the these folders:

    如果你的Rails版本是在> 3.1.0和<4之间,将你的字体放置在这些文件夹中:

    • app/assets/fonts
    • 应用程序/资产/字体
    • lib/assets/fonts
    • lib /资产/字体
    • vendor/assets/fonts
    • 供应商/资产/字体


    For Rails versions > 4, you must place your fonts in the app/assets/fonts folder.

    对于Rails版本> 4,必须将字体放置在app/assets/字体文件夹中。

    Note: To place fonts outside of these designated folders, use the following configuration:

    注意:要将字体放置在这些指定的文件夹之外,请使用以下配置:

    config.assets.precompile <

    config.assets。预编译<

    For Rails versions > 4.2, it is recommended to add this configuration to config/initializers/assets.rb.

    对于Rails版本的> 4.2,建议将此配置添加到config/initializer /assets.rb中。

    However, you can also add it to either config/application.rb , or to config/production.rb

    但是,您也可以将其添加到配置/应用程序中。rb,或配置/生产。rb。

  2. Declare your font in your CSS file:

    在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;
    }
    

    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.

    确保您的字体与声明的URL部分的名称完全相同。大写字母和标点符号很重要。在这种情况下,字体应该具有名称icomoon。

  3. If you are using Sass or Less with Rails > 3.1.0 (your CSS file has .scss or .less extension), then change the url(...) in the font declaration to font-url(...).

    如果您使用Sass或更少的Rails > . 3.1.0(您的CSS文件有.scss或. Less扩展),那么在字体声明中更改url(…)到font-url(…)。

    Otherwise, your CSS file should have the extension .css.erb, and the font declaration should be url('<%= asset_path(...) %>').

    否则,您的CSS文件应该有扩展名. CSS。erb和字体声明应该是url('<%= asset_path(…)%>')。

    If you are using Rails > 3.2.1, you can use font_path(...) instead of asset_path(...). This helper does exactly the same thing but it's more clear.

    如果您使用的是Rails > 3.2.1,您可以使用font_path(…)而不是asset_path(…)。这个帮助器做了同样的事情,但更清楚。

  4. 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:

    最后,在CSS中使用您的字体,就像您在font-family部分中声明的那样。如果它被宣布资本化,你可以这样使用它:

    font-family: 'Icomoon';
    

#2


35  

Now here's a twist:

现在这里有一个转折:

You should place all fonts in app/assets/fonts/ as they WILL get precompiled in staging and production by default—they will get precompiled when pushed to heroku.

你应该把所有的字体都放在app/assets/字体中,因为它们会被默认的在分期和生产中被预先编译,当被推到heroku时,它们会被预先编译。

Font files placed in vendor/assets will NOT be precompiled on staging or production by default — they will fail on heroku. Source!

在供应商/资产中放置的字体文件将不会在默认情况下被预先编译——它们将在heroku上失败。源!

— @plapier, thoughtbot/bourbon

——@plapier thoughtbot /波旁威士忌

I strongly believe that putting vendor fonts into vendor/assets/fonts makes a lot more sense than putting them into app/assets/fonts. With these 2 lines of extra configuration this has worked well for me (on Rails 4):

我坚信,将供应商的字体放到供应商/资产/字体中比将其放入应用程序/资产/字体中更有意义。通过这两行额外的配置,这对我很有效(在Rails 4上):

app.config.assets.paths <

— @jhilden, thoughtbot/bourbon

——@jhilden thoughtbot /波旁威士忌

I've also tested it on rails 4.0.0. Actually the last one line is enough to safely precompile fonts from vendor folder. Took a couple of hours to figure it out. Hope it helped someone.

我还在rails 4.0.0上测试了它。实际上,最后一行已经足够安全地预编译供应商文件夹中的字体了。花了几个小时才弄明白。希望它帮助某人。

#3


23  

If you don't want to keep track of moving your fonts around:

如果你不想记录移动你的字体:

# Adding Webfonts to the Asset Pipeline
config.assets.precompile <

#4


15  

You need to use font-url in your @font-face block, not url

您需要在@font-face块中使用fonturl,而不是url。

@font-face {
font-family: 'Inconsolata';
src:font-url('Inconsolata-Regular.ttf') format('truetype');
font-weight: normal;
font-style: normal;
}

as well as this line in application.rb, as you mentioned (for fonts in app/assets/fonts

以及在应用程序中的这一行。rb,正如您提到的(应用程序/资产/字体中的字体)。

config.assets.paths <

#5


9  

Here my approach to using fonts in asset pipeline:

下面是我在资产管道中使用字体的方法:

1) Put all your font file under app/assets/fonts/, actually you are not restricted to put it under fonts folder name. You can put any subfolder name you like. E.g. app/assets/abc or app/assets/anotherfonts. But i highly recommend you put it under app/assets/fonts/ for better folder structure.

1)把你所有的字体文件放在app/assets/字体下,实际上你不需要把它放在字体文件夹下。您可以添加任何您喜欢的子文件夹名称。如应用/资产/ abc或应用/资产/ anotherfonts。但是我强烈推荐你把它放在app/assets/字体/更好的文件夹结构中。

2) From your sass file, using the sass helper font-path to request your font assets like this

2)从您的sass文件中,使用sass助手的font-path来请求您的字体资产。

@font-face {
    font-family: 'FontAwesome';
    src: url(font-path('fontawesome-webfont.eot') + '?v=4.4.0');
    src: url(font-path('fontawesome-webfont.eot') + '?#iefix&v=4.4.0') format('embedded-opentype'),
         url(font-path('fontawesome-webfont.woff2') + '?v=4.4.0') format('woff2'),
         url(font-path('fontawesome-webfont.woff') + '?v=4.4.0') format('woff'),
         url(font-path('fontawesome-webfont.ttf') + '?v=4.4.0') format('truetype'),
         url(font-path('fontawesome-webfont.svg') + '?v=4.4.0#fontawesomeregular') format('svg');
    font-weight: normal;
    font-style: normal;
}

3) Run bundle exec rake assets:precompile from your local machine and see your application.css result. You should see something like this:

3)运行bundle exec rake资产:从本地机器预编译并查看应用程序。css的结果。你应该看到这样的东西:

@font-face {
    font-family: 'FontAwesome';
    src: url("/assets/fontawesome-webfont-d4f5a99224154f2a808e42a441ddc9248ffe78b7a4083684ce159270b30b912a.eot" "?v=4.4.0");
    src: url("/assets/fontawesome-webfont-d4f5a99224154f2a808e42a441ddc9248ffe78b7a4083684ce159270b30b912a.eot" "?#iefix&v=4.4.0") format("embedded-opentype"), url("/assets/fontawesome-webfont-3c4a1bb7ce3234407184f0d80cc4dec075e4ad616b44dcc5778e1cfb1bc24019.woff2" "?v=4.4.0") format("woff2"), url("/assets/fontawesome-webfont-a7c7e4930090e038a280fd61d88f0dc03dad4aeaedbd8c9be3dd9aa4c3b6f8d1.woff" "?v=4.4.0") format("woff"), url("/assets/fontawesome-webfont-1b7f3de49d68b01f415574ebb82e6110a1d09cda2071ad8451bdb5124131a292.ttf" "?v=4.4.0") format("truetype"), url("/assets/fontawesome-webfont-7414288c272f6cc10304aa18e89bf24fb30f40afd644623f425c2c3d71fbe06a.svg" "?v=4.4.0#fontawesomeregular") format("svg");
    font-weight: normal;
    font-style: normal;
}

If you want to know more how asset pipeline work, you can visit the following simple guide: https://designcode.commandrun.com/rails-asset-pipeline-simple-guide-830e2e666f6c#.6lejlayk2

如果您想了解更多关于资产管道的工作,您可以访问以下简单的指南:https://designcode.commandrun.com/rails-asset- simple-guide-830e2e666f6c#.6lejlayk2。

#6


5  

I'm using Rails 4.2, and could not get the footable icons to show up. Little boxes were showing, instead of the (+) on collapsed rows and the little sorting arrows I expected. After studying the information here, I made one simple change to my code: remove the font directory in css. That is, change all the css entries like this:

我使用的是Rails 4.2,并不能让footable图标出现。小框显示,而不是(+)在折叠的行和我所期望的小排序箭头。在研究了这里的信息之后,我对代码做了一个简单的修改:删除css中的字体目录。也就是说,修改所有css条目如下:

src:url('fonts/footable.eot');

to look like this:

看起来像这样:

src:url('footable.eot');

It worked. I think Rails 4.2 already assumes the font directory, so specifying it again in the css code makes the font files not get found. Hope this helps.

它工作。我认为Rails 4.2已经假定了字体目录,所以在css代码中再次指定它会使字体文件不被发现。希望这个有帮助。

#7


4  

I was having this problem on Rails 4.2 (with ruby 2.2.3) and had to edit the font-awesome _paths.scss partial to remove references to $fa-font-path and removing a leading forward slash. The following was broken:

我在Rails 4.2(使用ruby 2.2.3)上遇到了这个问题,并且不得不编辑那些令人生畏的路径。scss部分删除对$fa-font-path的引用并删除一个前导斜杠。以下被打破了:

@font-face {
  font-family: 'FontAwesome';
  src: font-url('#{$fa-font-path}/fontawesome-webfont.eot?v=#{$fa-version}');
  src: font-url('#{$fa-font-path}/fontawesome-webfont.eot?#iefix&v=#{$fa-version}') format('embedded-opentype'),
    font-url('#{$fa-font-path}/fontawesome-webfont.woff2?v=#{$fa-version}') format('woff2'),
    font-url('#{$fa-font-path}/fontawesome-webfont.woff?v=#{$fa-version}') format('woff'),
    font-url('#{$fa-font-path}/fontawesome-webfont.ttf?v=#{$fa-version}') format('truetype'),
    font-url('#{$fa-font-path}/fontawesome-webfont.svg?v=#{$fa-version}#fontawesomeregular') format('svg');
  font-weight: normal;
  font-style: normal;
}

And the following works:

以下工作:

@font-face {
  font-family: 'FontAwesome';
  src: font-url('fontawesome-webfont.eot?v=#{$fa-version}');
  src: font-url('fontawesome-webfont.eot?#iefix&v=#{$fa-version}') format('embedded-opentype'),
    font-url('fontawesome-webfont.woff2?v=#{$fa-version}') format('woff2'),
    font-url('fontawesome-webfont.woff?v=#{$fa-version}') format('woff'),
    font-url('fontawesome-webfont.ttf?v=#{$fa-version}') format('truetype'),
    font-url('fontawesome-webfont.svg?v=#{$fa-version}#fontawesomeregular') format('svg');
  font-weight: normal;
  font-style: normal;
}

An alternative would be to simply remove the forward slash following the interpolated $fa-font-path and then define $fa-font-path as an empty string or subdirectory with trailing forward slash (as needed).

另一种方法是简单地删除插入$fa-font-path后的正斜杠,然后将$fa-font-path定义为一个空字符串或子目录,后面是斜杠(根据需要)。

Remember to recompile assets and restart your server as needed. For example, on a passenger setup:

记住要重新编译资产并根据需要重新启动服务器。例如,在乘客设置上:

prompt> rake assets:clean; rake assets:clobber
prompt> RAILS_ENV=production RAILS_GROUPS=assets rake assets:precompile
prompt> service passenger restart

Then reload your browser.

然后重新加载浏览器。

#8


2  

I had a similar issue when I upgraded my Rails 3 app to Rails 4 recently. My fonts were not working properly as in the Rails 4+, we are only allowed to keep the fonts under app/assets/fonts directory. But my Rails 3 app had a different font organization. So I had to configure the app so that it still works with Rails 4+ having my fonts in a different place other than app/assets/fonts. I have tried several solutions but after I found non-stupid-digest-assets gem, it just made it so easy.

最近我把Rails 3应用升级到Rails 4时也遇到了类似的问题。我的字体没有像Rails 4+那样正常工作,我们只允许在app/assets/字体目录下保留字体。但是我的Rails 3应用有一个不同的字体组织。因此,我必须配置这个应用程序,这样它仍然可以使用Rails 4+在不同的地方使用我的字体,而不是应用程序/资产/字体。我已经尝试了几种解决方案,但在我发现了非愚蠢的资产之后,它就变得很简单了。

Add this gem by adding the following line to your Gemfile:

在Gemfile中添加以下一行,添加这个gem:

gem 'non-stupid-digest-assets'

Then run:

然后运行:

bundle install

And finally add the following line in your config/initializers/non_digest_assets.rb file:

最后,在配置/初始化器/non_digest_assets中添加以下行。rb文件:

NonStupidDigestAssets.whitelist = [ /\.(?:svg|eot|woff|ttf)$/ ]

That's it. This solved my problem nicely. Hope this helps someone who have encountered similar problem like me.

就是这样。这很好地解决了我的问题。希望这能帮助遇到类似问题的人。

#9


1  

If you have a file called scaffolds.css.scss, then there's a chance that's overriding all the custom things you're doing in the other files. I commented out that file and suddenly everything worked. If there isn't anything important in that file, you might as well just delete it!

如果你有一个叫做脚手架。css的文件。scss,还有一个可能是重写所有你在其他文件中做的自定义的事情。我把文件注释掉了,突然一切都好了。如果这个文件中没有什么重要的东西,你可以删掉它!

#10


1  

In my case the original question was using asset-url without results instead of plain url css property. Using asset-url ended up working for me in Heroku. Plus setting the fonts in /assets/fonts folder and calling asset-url('font.eot') without adding any subfolder or any other configuration to it.

在我的例子中,最初的问题是使用没有结果的资产url,而不是简单的url css属性。使用资产url最终在Heroku为我工作。另外,在不添加任何子文件夹或任何其他配置的情况下,将字体设置为/assets/字体文件夹并调用资产url('font.eot')。

#11


-6  

just place your fonts inside app/assets/fonts folder and set the autoload path when app start using writing the code in application.rb

只要把你的字体放在app/assets/字体文件夹里,然后在应用程序开始使用代码的时候设置autoload路径。

config.assets.paths < and

config.assets。<

then use the following code in css.

然后在css中使用下面的代码。

@font-face {

@font-face {

 font-family: 'icomoon';
 src: asset-url('icomoon.eot');
 src: asset-url('icomoon.eot') format('embedded-opentype'),
      asset-url('icomoon.woff') format('woff'),
      asset-url('icomoon.ttf') format('truetype'),
      asset-url('icomoon.svg') format('svg');
 font-weight: normal;
 font-style: normal;

}

}

Give it a try.

试一试。

Thanks

谢谢


推荐阅读
  • 使用eclipse创建一个Java项目的步骤
    本文介绍了使用eclipse创建一个Java项目的步骤,包括启动eclipse、选择New Project命令、在对话框中输入项目名称等。同时还介绍了Java Settings对话框中的一些选项,以及如何修改Java程序的输出目录。 ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • Linux重启网络命令实例及关机和重启示例教程
    本文介绍了Linux系统中重启网络命令的实例,以及使用不同方式关机和重启系统的示例教程。包括使用图形界面和控制台访问系统的方法,以及使用shutdown命令进行系统关机和重启的句法和用法。 ... [详细]
  • 开发笔记:加密&json&StringIO模块&BytesIO模块
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了加密&json&StringIO模块&BytesIO模块相关的知识,希望对你有一定的参考价值。一、加密加密 ... [详细]
  • 本文介绍了C#中生成随机数的三种方法,并分析了其中存在的问题。首先介绍了使用Random类生成随机数的默认方法,但在高并发情况下可能会出现重复的情况。接着通过循环生成了一系列随机数,进一步突显了这个问题。文章指出,随机数生成在任何编程语言中都是必备的功能,但Random类生成的随机数并不可靠。最后,提出了需要寻找其他可靠的随机数生成方法的建议。 ... [详细]
  • 原文地址:https:www.cnblogs.combaoyipSpringBoot_YML.html1.在springboot中,有两种配置文件,一种 ... [详细]
  • 如何用UE4制作2D游戏文档——计算篇
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了如何用UE4制作2D游戏文档——计算篇相关的知识,希望对你有一定的参考价值。 ... [详细]
  • 1,关于死锁的理解死锁,我们可以简单的理解为是两个线程同时使用同一资源,两个线程又得不到相应的资源而造成永无相互等待的情况。 2,模拟死锁背景介绍:我们创建一个朋友 ... [详细]
  • XML介绍与使用的概述及标签规则
    本文介绍了XML的基本概念和用途,包括XML的可扩展性和标签的自定义特性。同时还详细解释了XML标签的规则,包括标签的尖括号和合法标识符的组成,标签必须成对出现的原则以及特殊标签的使用方法。通过本文的阅读,读者可以对XML的基本知识有一个全面的了解。 ... [详细]
  • Android源码深入理解JNI技术的概述和应用
    本文介绍了Android源码中的JNI技术,包括概述和应用。JNI是Java Native Interface的缩写,是一种技术,可以实现Java程序调用Native语言写的函数,以及Native程序调用Java层的函数。在Android平台上,JNI充当了连接Java世界和Native世界的桥梁。本文通过分析Android源码中的相关文件和位置,深入探讨了JNI技术在Android开发中的重要性和应用场景。 ... [详细]
  • 本文介绍了深入浅出Linux设备驱动编程的重要性,以及两种加载和删除Linux内核模块的方法。通过一个内核模块的例子,展示了模块的编译和加载过程,并讨论了模块对内核大小的控制。深入理解Linux设备驱动编程对于开发者来说非常重要。 ... [详细]
  • 本文介绍了在wepy中运用小顺序页面受权的计划,包含了用户点击作废后的从新受权计划。 ... [详细]
  • Java实战之电影在线观看系统的实现
    本文介绍了Java实战之电影在线观看系统的实现过程。首先对项目进行了简述,然后展示了系统的效果图。接着介绍了系统的核心代码,包括后台用户管理控制器、电影管理控制器和前台电影控制器。最后对项目的环境配置和使用的技术进行了说明,包括JSP、Spring、SpringMVC、MyBatis、html、css、JavaScript、JQuery、Ajax、layui和maven等。 ... [详细]
  • Google Play推出全新的应用内评价API,帮助开发者获取更多优质用户反馈。用户每天在Google Play上发表数百万条评论,这有助于开发者了解用户喜好和改进需求。开发者可以选择在适当的时间请求用户撰写评论,以获得全面而有用的反馈。全新应用内评价功能让用户无需返回应用详情页面即可发表评论,提升用户体验。 ... [详细]
author-avatar
美丽空间wo
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有