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 <
我有添加config.assets。< and the compile CSS source is as follows: 编译CSS源代码如下: 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控制台时,我得到以下内容: 602 If your Rails version is between 如果你的Rails版本是在> 3.1.0和<4之间,将你的字体放置在这些文件夹中: For Rails versions 对于Rails版本> 4,必须将字体放置在app/assets/字体文件夹中。 Note: To place fonts outside of these designated folders, use the following configuration: 注意:要将字体放置在这些指定的文件夹之外,请使用以下配置: config.assets。预编译< \。(?:svg |测试结束woff | | ttf)\ z / For Rails versions 对于Rails版本的> 4.2,建议将此配置添加到config/initializer /assets.rb中。 However, you can also add it to either 但是,您也可以将其添加到配置/应用程序中。rb,或配置/生产。rb。 Declare your font in your CSS file: 在CSS文件中声明你的字体: 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 确保您的字体与声明的URL部分的名称完全相同。大写字母和标点符号很重要。在这种情况下,字体应该具有名称icomoon。 If you are using Sass or Less with Rails 如果您使用Sass或更少的Rails > . 3.1.0(您的CSS文件有.scss或. Less扩展),那么在字体声明中更改url(…)到font-url(…)。 Otherwise, your CSS file should have the extension 否则,您的CSS文件应该有扩展名. CSS。erb和字体声明应该是url('<%= asset_path(…)%>')。 If you are using Rails 如果您使用的是Rails > 3.2.1,您可以使用font_path(…)而不是asset_path(…)。这个帮助器做了同样的事情,但更清楚。 Finally, use your font in your CSS like you declared it in the 最后,在CSS中使用您的字体,就像您在font-family部分中声明的那样。如果它被宣布资本化,你可以这样使用它: 35 Now here's a twist: 现在这里有一个转折: You should place all fonts in 你应该把所有的字体都放在app/assets/字体中,因为它们会被默认的在分期和生产中被预先编译,当被推到heroku时,它们会被预先编译。 Font files placed in 在供应商/资产中放置的字体文件将不会在默认情况下被预先编译——它们将在heroku上失败。源! — @plapier, thoughtbot/bourbon ——@plapier thoughtbot /波旁威士忌 I strongly believe that putting vendor fonts into 我坚信,将供应商的字体放到供应商/资产/字体中比将其放入应用程序/资产/字体中更有意义。通过这两行额外的配置,这对我很有效(在Rails 4上): — @jhilden, thoughtbot/bourbon ——@jhilden thoughtbot /波旁威士忌 I've also tested it on 我还在rails 4.0.0上测试了它。实际上,最后一行已经足够安全地预编译供应商文件夹中的字体了。花了几个小时才弄明白。希望它帮助某人。 23 If you don't want to keep track of moving your fonts around: 如果你不想记录移动你的字体: 15 You need to use 您需要在@font-face块中使用fonturl,而不是url。 as well as this line in application.rb, as you mentioned (for fonts in 以及在应用程序中的这一行。rb,正如您提到的(应用程序/资产/字体中的字体)。 9 Here my approach to using fonts in asset pipeline: 下面是我在资产管道中使用字体的方法: 1) Put all your font file under 1)把你所有的字体文件放在app/assets/字体下,实际上你不需要把它放在字体文件夹下。您可以添加任何您喜欢的子文件夹名称。如应用/资产/ abc或应用/资产/ anotherfonts。但是我强烈推荐你把它放在app/assets/字体/更好的文件夹结构中。 2) From your sass file, using the sass helper 2)从您的sass文件中,使用sass助手的font-path来请求您的字体资产。 3) Run 3)运行bundle exec rake资产:从本地机器预编译并查看应用程序。css的结果。你应该看到这样的东西: 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。 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条目如下: to look like this: 看起来像这样: 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代码中再次指定它会使字体文件不被发现。希望这个有帮助。 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 我在Rails 4.2(使用ruby 2.2.3)上遇到了这个问题,并且不得不编辑那些令人生畏的路径。scss部分删除对$fa-font-path的引用并删除一个前导斜杠。以下被打破了: And the following works: 以下工作: An alternative would be to simply remove the forward slash following the interpolated 另一种方法是简单地删除插入$fa-font-path后的正斜杠,然后将$fa-font-path定义为一个空字符串或子目录,后面是斜杠(根据需要)。 Remember to recompile assets and restart your server as needed. For example, on a passenger setup: 记住要重新编译资产并根据需要重新启动服务器。例如,在乘客设置上: Then reload your browser. 然后重新加载浏览器。 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 最近我把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: Then run: 然后运行: And finally add the following line in your config/initializers/non_digest_assets.rb file: 最后,在配置/初始化器/non_digest_assets中添加以下行。rb文件: That's it. This solved my problem nicely. Hope this helps someone who have encountered similar problem like me. 就是这样。这很好地解决了我的问题。希望这能帮助遇到类似问题的人。 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,还有一个可能是重写所有你在其他文件中做的自定义的事情。我把文件注释掉了,突然一切都好了。如果这个文件中没有什么重要的东西,你可以删掉它! 1 In my case the original question was using 在我的例子中,最初的问题是使用没有结果的资产url,而不是简单的url css属性。使用资产url最终在Heroku为我工作。另外,在不添加任何子文件夹或任何其他配置的情况下,将字体设置为/assets/字体文件夹并调用资产url('font.eot')。 -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 < config.assets。< then use the following code in css. 然后在css中使用下面的代码。 @font-face { @font-face { } } Give it a try. 试一试。 Thanks 谢谢@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");
}
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
> 3.1.0
and <4
, place your fonts in any of the these folders:
app/assets/fonts
lib/assets/fonts
vendor/assets/fonts
> 4
, you must place your fonts in the app/assets/fonts
folder.config.assets.precompile <\.(?:svg|eot|woff|ttf)\z/
> 4.2
, it is recommended to add this configuration to config/initializers/assets.rb
. config/application.rb
, or to config/production.rb
@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;
}
icomoon
.> 3.1.0
(your CSS file has .scss
or .less
extension), then change the url(...)
in the font declaration to font-url(...)
..css.erb
, and the font declaration should be url('<%= asset_path(...) %>')
.> 3.2.1
, you can use font_path(...)
instead of asset_path(...)
. This helper does exactly the same thing but it's more clear.font-family
part. If it was declared capitalized, you can use it like this:font-family: 'Icomoon';
#2
app/assets/fonts/
as they WILL get precompiled in staging and production by default—they will get precompiled when pushed to heroku.vendor/assets
will NOT be precompiled on staging or production by default — they will fail on heroku. Source!
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):app.config.assets.paths <
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.#3
# Adding Webfonts to the Asset Pipeline
config.assets.precompile <
#4
font-url
in your @font-face block, not url
@font-face {
font-family: 'Inconsolata';
src:font-url('Inconsolata-Regular.ttf') format('truetype');
font-weight: normal;
font-style: normal;
}
app/assets/fonts
config.assets.paths <
#5
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.font-path
to request your font assets like this @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;
}
bundle exec rake assets:precompile
from your local machine and see your application.css result. You should see something like this:@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;
}
#6
src:url('fonts/footable.eot');
src:url('footable.eot');
#7
$fa-font-path
and removing a leading forward slash. The following was broken:@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;
}
@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;
}
$fa-font-path
and then define $fa-font-path
as an empty string or subdirectory with trailing forward slash (as needed).prompt> rake assets:clean; rake assets:clobber
prompt> RAILS_ENV=production RAILS_GROUPS=assets rake assets:precompile
prompt> service passenger restart
#8
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.gem 'non-stupid-digest-assets'
bundle install
NonStupidDigestAssets.whitelist = [ /\.(?:svg|eot|woff|ttf)$/ ]
#9
#10
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.#11
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;