【问题标题】:Font-Face not Working with Rails 3.1字体不能与 Rails 3.1 一起使用
【发布时间】:2012-03-27 15:11:29
【问题描述】:

我刚刚升级到 Rails 3.1 和 Asset Pipeline,但我无法弄清楚为什么我的字体不再被读取。我已经尝试了这篇文章中的答案(没有运气): Using @font-face with Rails 3.1 app?

目前,我正在尝试选择的解决方案。我在 app/assets 下有一个字体文件夹。文件名正确且存在于字体目录中。

在我的 Application.rb 中

config.assets.paths << "#{Rails.root}/app/assets/fonts"

我也尝试过,来自 Rails 指南 (http://guides.rubyonrails.org/asset_pipeline.html):

config.assets.paths << Rails.root.join("app", "assets", "fonts")

使用此代码,运行代码时路径映射到此 src: url('/assets/League_Gothic-webfont.eot

在我的 CSS 中:

@font-face {
  font-family: "League_Gothic";
  src: url('<%= asset_path('League_Gothic-webfont.eot') %>');
  font-weight: normal;
  font-style: normal;
}

当我尝试其他解决方案时,基本上是对路径进行硬编码:

src: url(/assets/fonts/League_Gothic-webfont.eot);

单击页面源代码中的链接时出现此错误:

No route matches [GET] "/assets/fonts/League_Gothic-webfont.eot"   

【问题讨论】:

    标签: ruby-on-rails font-face


    【解决方案1】:

    将此添加到 application.rb

    # Enable the asset pipeline
    config.assets.enabled = true
    config.assets.paths << "#{Rails.root}/vendor/assets/fonts"
    # Precompile additional assets
    config.assets.precompile += %w( .svg .eot .woff .ttf )
    

    然后在使用字体之前将此添加到css或scss中

    @font-face {
      font-family: 'Museo300Regular';
      src:  font-url('Museo300-Regular-webfont.eot');
      src:  local('Museo300Regular'),
            font-url('Museo300-Regular-webfont.eot?#iefix') format("embedded-opentype"),
            font-url('Museo300-Regular-webfont.woff') format("woff"),
            font-url('Museo300-Regular-webfont.ttf') format("truetype"),
            font-url('Museo300-Regular-webfont.svg#Museo300Regular') format("svg") ;
      font-weight: normal;
      font-style: normal;
    }
    

    【讨论】:

    • 工作就像一个魅力!谢谢。
    【解决方案2】:

    您不必在 application.rb 中进行这些更改。

    但是您的问题可能是您正在执行“assets/fonts/myfont.eof”,而您应该执行“assets/myfont.eof”。如果它们是资产,请不要处理字体、图像等目录名称,只需从 assets/ 中调用它们即可

    您可能必须删除您在 application.rb 中更改的内容才能使其正常工作。不知道,有没有都试试。


    另外,来自我正在处理的项目的 CSS 文件:

    @font-face
    {
      font-family: ubuntu;
      src: url("/assets/Ubuntu-R.ttf");
    }
    

    这可能会有所帮助。

    【讨论】:

    • 你是对的,但并不完全正确。在我的 Rails 3.2 项目中,我刚刚做了 src: url("league_gothic-webfont.eot");没有“assets”或“assets/fonts”并且它只是工作,即使资产的路径是 vendor/assets/fonts/league_gothic-webfont.eot
    • @idrinkpabst 我认为这是针对 Rails 3.1 的,与您提到的 Rails 3.2 项目不同
    猜你喜欢
    • 2020-08-05
    • 2018-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-16
    • 2012-12-10
    • 2011-12-19
    • 2014-03-07
    相关资源
    最近更新 更多