FontAwesome与Rails 4.0.1不兼容。

12

我正在使用fontawesome 3.2.1和bootstrap 3.0.0在我的rails 4.0.1项目中。所有我的资产都位于vendor/assets中。

问题是当我编译我的资产(生产环境)并在生产环境下运行服务器时,我的fontawesome只能在开发模式下工作。错误如下:

Started GET "/assets/fontawesome-webfont.svg" for 127.0.0.1 at 2014-01-08 11:48:55 +0530

ActionController::RoutingError (No route matches [GET] "/assets/fontawesome-webfont.svg"):
 actionpack (4.0.1) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
 actionpack (4.0.1) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'

资产如下

$ls vendor/assets/
=> fonts  images  javascripts  stylesheets

$ls vendor/assets/*
=> vendor/assets/fonts:
FontAwesome.otf          fontawesome-webfont.ttf           glyphicons-halflings-   regular.svg
fontawesome-webfont.eot  fontawesome-webfont.woff          glyphicons-halflings- regular.ttf
fontawesome-webfont.svg  glyphicons-halflings-regular.eot  glyphicons-halflings-regular.woff

vendor/assets/images:
bg_direction_nav.png  bxslider  search-icon.jpg

vendor/assets/javascripts:
bootstrap  bxslider  fancybox  others  revolution_slider

vendor/assets/stylesheets:
bootstrap  bxslider  fancybox  font_awesome  others  revolution_slider

 $ls vendor/assets/stylesheets/bootstrap/
 => bootstrap.min.css

 $ls vendor/assets/stylesheets/font_awesome/
 => font-awesome.css

我的application.css如下:

$cat app/assets/stylesheets/application.css 
/*
* This is a manifest file that'll be compiled into application.css, which will include   all the files
* listed below.
*
* Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
* or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path.
*
* You're free to add application-wide styles to this file and they'll appear at the top of the
* compiled file, but it's generally better to create a new file per style scope.
*
*= require bootstrap/bootstrap.min.css
*= require others/theme.css
*= require others/bootstrap-reset.css
*= require font_awesome/font-awesome.css
*= require bxslider/jquery.bxslider.css
*= require fancybox/jquery.fancybox.css
*= require revolution_slider/rs-style.css
*= require revolution_slider/settings.css
*= require others/flexslider.css 
*= require others/style.css
*= require others/style-responsive.css
*= require_self
*/

fontawesome在font-awesome.css中加载

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

字形图标被加载在bootstrap.min.css中

@font-face{
  font-family:'Glyphicons Halflings';
  src:url('/assets/glyphicons-halflings-regular.eot');
  src:url('/assets/glyphicons-halflings-regular.eot?#iefix') format('embedded-Opentype'), url('/assets/glyphicons-halflings-regular.woff') format('woff'),url('/assets/glyphicons-halflings-regular.ttf') format('truetype'),url('/assets/glyphicons-halflings-regular.svg#glyphicons-halflingsregular') format('svg')}
我尝试了一些解决方案,例如在"url"前添加'font'或'assets',但都没有成功。
--更新
config/application.rb的内容。
config.assets.enabled = true
config.assets.version = '1.0'

config.assets.paths += ["#{config.root}/vendor/assets/fonts", "#config.root}/app/assets/images/**", "#{config.root}/vendor/assets/images"]
config.assets.precompile += %w(*.png *.jpg *.jpeg *.gif *.eot *.svg *.ttf *.otf *.woff vendor/assets/stylesheets/**/* vendor/assets/fonts/*)

["#{config.root}/vendor/assets/javascripts", "#config.root}/vendor/assets/stylesheets"].each do |d|
config.assets.precompile += Dir.glob("#{d}/*").map{|f| "#{f.gsub(d + '/', '')}/**/*" if File.directory?(f)}.compact
2个回答

10

我按照以下规定解决了问题。

1)首先,在.css文件中不要引用资源。将该文件重命名为.css.scss。这与创建控制器并为其创建资产(JavaScript 和 CSS)的扩展名相同。如果 CSS 文件很大,可以使用多个应用程序将 CSS 转换为 SCSS。

2)在样式表中引用字体时,请使用font-path。也就是说,使用 Rails 提供的方法引用资源,而不是使用“src”。例如:

@font-face {
  font-family: 'MuseoSans-700';
  src: url(font-path('museoSans/244DD4_0_0.eot'));
  src: url(font-path('museoSans/244DD4_0_0.eot?#iefix')) format('embedded-opentype');
  src: url(font-path('museoSans/244DD4_0_0.woff')) format('woff');
  src: url(font-path('museoSans/244DD4_0_0.ttf')) format('truetype');
}

在上面的代码中,我的文件是"app/assets/stylesheets/fonts.scss",而我的字体位于

$ls vendor/assets/fonts/
  fontawesome  museoSans  museoSans500
$ls vendor/assets/fonts/museoSans
  244DD4_0_0.eot   244DD4_1_0.ttf   244DD4_2_0.woff  244DD4_4_0.eot   244DD4_5_0.ttf   244DD4_6_0.woff  244DD4_8_0.eot   244DD4_9_0.ttf
  244DD4_0_0.ttf   244DD4_1_0.woff  244DD4_3_0.eot   244DD4_4_0.ttf   244DD4_5_0.woff  244DD4_7_0.eot   244DD4_8_0.ttf   244DD4_9_0.woff
  244DD4_0_0.woff  244DD4_2_0.eot   244DD4_3_0.ttf   244DD4_4_0.woff  244DD4_6_0.eot   244DD4_7_0.ttf   244DD4_8_0.woff
  244DD4_1_0.eot   244DD4_2_0.ttf   244DD4_3_0.woff  244DD4_5_0.eot   244DD4_6_0.ttf   244DD4_7_0.woff  244DD4_9_0.eot

3)在资产预编译期间,“font-path”或“asset-path”等帮助程序可以帮助指向正确的资产位置。例如,如果您将其指定为

src: url('/assets/museoSans/244DD4_0_0.eot'));

在编译的资产中,这个仍然会保持一致。理想情况下应该是

src: url('/assets/museoSans/244DD4_0_0-67652745236457645234dghfhsagfd64354.eot'));

您可以使用“ grep”命令检查编译后的文件,位于“ public / assets” 中。

4)确保资产正在加载的另一种方法是从 URL 调用它们。

5)请在生产或测试环境文件中设置“ config.assets.compress = false”,并在生产/测试环境中本地运行应用程序,以便检查视图以确保资产正在加载。

更多了解,请查看https://github.com/joshsoftware/website/commit/859f2709180e9fb0aac59549d64bd4351a2842b3


1
我尝试修复Bootstrap字形图标(超过2小时),但是font-path是最终使应用程序中的字体定义覆盖开始在生产环境中工作的技巧。 - David Krider
@DavidKrider 将 application.css 重命名为 application.scss。 - Prasad Surase

5

您的SVG似乎没有编译,指纹似乎丢失,除非您设置:

config.assets.digest = false 

在预编译后,我一直遇到相同的“没有匹配的GET路由”问题。您可以尝试在config/application.rb中添加SVG。
config.assets.precompile += %w(*.svg)

然后重试a:

rake assets:precompile RAILS_ENV=production

在我的情况下,简单的“rake assets:precompile”不够用,我需要指定RAILS_ENV和RAILS_GROUPS(如果Rails版本低于4)。
通常编译后的资产可以在public/assets目录中找到。
希望这有所帮助。
更新:
根据我在使用Rails 4生产环境中的ckeditor的经验,另一个想法是资产编译与摘要不起作用(https://github.com/galetahub/ckeditor)。你需要添加一个rake任务,在编译后复制/修改一些文件到public/assets目录中。以下是代码:
namespace :ckeditor do
  desc 'Create nondigest versions of some ckeditor assets (e.g. moono skin png)'
  task :create_nondigest_assets do
    fingerprint = /\-[0-9a-f]{32}\./
    for file in Dir['public/assets/ckeditor/contents-*.css', 'public/assets/ckeditor/skins/moono/*.png']
      next unless file =~ fingerprint
      nondigest = file.sub fingerprint, '.' # contents-0d8ffa186a00f5063461bc0ba0d96087.css => contents.css
      FileUtils.cp file, nondigest, verbose: true
    end
  end
end

# auto run ckeditor:create_nondigest_assets after assets:precompile
Rake::Task['assets:precompile'].enhance do
  Rake::Task['ckeditor:create_nondigest_assets'].invoke
end

你可以轻松地用fontawsome替换ckeditor。

确实,但在我的情况下,就像我提到的那样,一个简单的rake assets:precompile是不够的(我记得一些bootstrap元素和ckeditor没有被预编译)。也许指定ENV就足够了,但我无法解释为什么会出现这种差异。你尝试在config/application.rb中添加config.assets.precompile += %w(*.svg)了吗? - coding addicted
在我的情况下,上次出现“无路由匹配”的问题是因为编译缺少一些文件。您可以检查清单以查看fontawsome是否已正确编译(public/assets/manifest-fingerprint.json)。我有另一个想法,我正在更新我的答案。 - coding addicted
另一个问题,你是否使用fontawesome的gem?如果是,你介意分享一个链接,这样我就可以在一个空项目中尝试一下了吗? - coding addicted
我将创建一个空白项目,只包含最少量的数据,并在此处发布Git仓库的URL。 - Prasad Surase
我还在看你的问题(抱歉打扰了)。我刚刚看了另一篇关于fontawesome的帖子,建议将font-awesome.css重命名为font-awesome.css.erb并进行修改。这样Rails就可以处理任何环境下的路径。这也可能是一个解决方案。帖子链接:https://dev59.com/yGgu5IYBdhLWcg3w0qNK - coding addicted
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接