2013-10-10 35 views
6

在我的rails 4项目css中使用字体文件。所以他们需要额外的预编译。预编译没有md5指纹的特定资产

我达到与添加下列行来配置/环境/ production.rb

# Add the fonts path 
    config.assets.paths << Rails.root.join('vendor', 'assets', 'fonts','fonts') 
    # 
    # # Precompile additional assets 
    config.assets.precompile += %w(*.svg *.eot *.woff *.ttf) 

和运行耙资产:预编译的生产。

然而,结果如下:

I, [2013-10-10T19:27:51.931963 #16052] INFO -- : Writing /var/lib/openshift/521e19c85004460a8e000107/app-root/runtime/repo/public/assets/fonts/glyphicons-halflings-regular-ab2f6984951c07fd89e6afdefabd93c7.eot 
I, [2013-10-10T19:27:51.940615 #16052] INFO -- : Writing /var/lib/openshift/521e19c85004460a8e000107/app-root/runtime/repo/public/assets/fonts/glyphicons-halflings-regular-24dfb40c91db789b8b8faba6886ac1ef.svg 
I, [2013-10-10T19:27:51.950685 #16052] INFO -- : Writing /var/lib/openshift/521e19c85004460a8e000107/app-root/runtime/repo/public/assets/fonts/glyphicons-halflings-regular-4b2130768da98222338d1519f9179528.ttf 
I, [2013-10-10T19:27:51.983230 #16052] INFO -- : Writing /var/lib/openshift/521e19c85004460a8e000107/app-root/runtime/repo/public/assets/fonts/glyphicons-halflings-regular-7a07f26f72466361ac9671de2d33fd1c.woff 

和CSS文件是指字体文件没有这个MD5指纹。

我该如何预编译资产,以便它们在没有md5指纹的情况下生成?或者我应该把它们放在公共/字体/文件夹在这种情况下?

+2

你解决了吗?我有同样的问题.. –

+0

我的答案是否有效? – Ludovic

回答

2

按照以下步骤

  • 你的字体必须在app/assets/fonts
  • 添加您的字体资产路径(像你这样),但更喜欢config/application.rb
  • 与声明的字体在你的CSS @font-face。您会发现some help here
  • 如果您不使用SCSS,则必须嵌入css,例如application.css.erb,并使用asset_path()助手在您的font-face声明中实现您的字体路径。

个例无SCSS:

@font-face { 
    font-family: 'MyFont'; 
    src:url('<%= asset_path("myfont.eot")%>'); 
    src:url('<%= asset_path("myfont.eot?#iefix")%>') format('embedded-opentype'), 
    url('<%= asset_path("myfont.svg#myfont")%>') format('svg'), 
    url('<%= asset_path("myfont.woff")%>') format('woff'), 
    url('<%= asset_path("myfont.ttf")%>') format('truetype'); 
    font-weight: normal; 
    font-style: normal; 
} 
+2

问题不在于如何在创建字体时创建字体,而是使用md5指纹创建生产字体的预编译资源。但在网络控制台之后有fontawesome-webfont-62877.woff 404错误。它看起来像Rails没有看到publick/assets fontawesome-webfont-62877-e70f92449ebfddada3d455eb44542655.woff中的预编译资产,但是当我添加文件fontawesome-webfont-62877.woff没有md5时,它工作正常。如何创建预编译的rails可以看到生产中的字体。 –

+0

@SergeyChechaev用我的方法,一切工作在生产模式。随着rails3我有问题,解决方案是复制public/assets /中的字体目录。如果您不想预编译字体,为什么要将它们添加到资产系统中?这有点奇怪:) – Ludovic

+0

我不明白如何在生产模式下预编译资产,它是叉罚款我有404错误。 –