热门标签 | 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

谢谢


推荐阅读
  • iOS 开发技巧:TabBarController 自定义与本地通知设置
    本文介绍了如何在 iOS 中自定义 TabBarController 的背景颜色和选中项的颜色,以及如何使用本地通知设置应用程序图标上的提醒个数。通过这些技巧,可以提升应用的用户体验。 ... [详细]
  • ListView简单使用
    先上效果:主要实现了Listview的绑定和点击事件。项目资源结构如下:先创建一个动物类,用来装载数据:Animal类如下:packagecom.example.simplelis ... [详细]
  • 本文详细介绍了get和set方法的作用及其在编程中的实现方式,同时探讨了点语法的使用场景。通过具体示例,解释了属性声明与合成存取方法的概念,并补充了相关操作的最佳实践。 ... [详细]
  • 本文探讨了如何利用HTML5和JavaScript在浏览器中进行本地文件的读取和写入操作,并介绍了获取本地文件路径的方法。HTML5提供了一系列API,使得这些操作变得更加简便和安全。 ... [详细]
  • 深入剖析JVM垃圾回收机制
    本文详细探讨了Java虚拟机(JVM)中的垃圾回收机制,包括其意义、对象判定方法、引用类型、常见垃圾收集算法以及各种垃圾收集器的特点和工作原理。通过理解这些内容,开发人员可以更好地优化内存管理和程序性能。 ... [详细]
  • 本文介绍了如何通过在数据库表中增加一个字段来记录文章的访问次数,并提供了一个示例方法用于更新该字段值。 ... [详细]
  • 解决Spring Boot项目创建失败的问题
    在尝试创建新的Spring Boot项目时遇到了一些问题,具体表现为在项目创建过程中的两个关键步骤出现错误。本文将详细探讨这些问题及其解决方案。 ... [详细]
  • 一个登陆界面
    预览截图html部分123456789101112用户登入1314邮箱名称邮箱为空15密码密码为空16登 ... [详细]
  • 使用WinForms 实现 RabbitMQ RPC 示例
    本文通过两个WinForms应用程序演示了如何使用RabbitMQ实现远程过程调用(RPC)。一个应用作为客户端发送请求,另一个应用作为服务端处理请求并返回响应。 ... [详细]
  • 本文介绍了如何使用JFreeChart库创建一个美观且功能丰富的环形图。通过设置主题、字体和颜色等属性,可以生成符合特定需求的图表。 ... [详细]
  • 在使用Rails Paperclip插件与AWS S3进行文件管理时,遇到了`exists?`和`clear`方法调用时出现的`AWS::S3::Errors::BadRequest`错误。本文探讨了问题的原因及可能的解决方案。 ... [详细]
  • 开发笔记:Bash:从一个引用字符串块中的每个带引号的字符串中创建一个变量 ... [详细]
  • KKCMS代码审计初探
    本文主要介绍了KKCMS的安装过程及其基本功能,重点分析了该系统中存在的验证码重用、SQL注入及XSS等安全问题。适合初学者作为入门指南。 ... [详细]
  • 本文实例为大家分享了Python实现代码统计工具的具体代码,供大家参考,具体内容如下思路:首先获取所有文件,然后统计每个文 ... [详细]
  • 深入理解Play Framework 1.2.7中的缓存机制
    本文探讨了Play Framework 1.2.7版本中提供的缓存解决方案,包括Ehcache和Memcached的集成与使用。文章详细介绍了缓存相关的类及其功能,以及如何通过配置选择合适的缓存实现。 ... [详细]
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社区 版权所有