【问题标题】:After precompiling assets for production, I get "File to import not found or unreadable: fonts." error in development在为生产预编译资产后,我得到“找不到或无法读取要导入的文件:字体”。开发中的错误
【发布时间】:2014-03-21 15:01:29
【问题描述】:

我有一个 Rails 4 应用程序,在 assets 文件夹中有一个字体目录,还有一些常用的东西。我为生产预编译了我的资产,但现在当我尝试在本地工作时,我得到:

Sass::SyntaxError
File to import not found or unreadable: fonts.

整个错误如下所示:

没有太多运气找到答案。谢谢。

编辑

我尝试将我的资产移动到 gemfile 中的资产块中,但没有任何效果。在此之前他们不在任何街区,也不在那里工作。

application.rb

require File.expand_path('../boot', __FILE__)

require 'rails/all'

# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)

module Atbp
  class Application < Rails::Application
    config.assets.paths << Rails.root.join("app", "assets", "fonts")
  end
end

【问题讨论】:

    标签: ruby-on-rails sass asset-pipeline zurb-foundation


    【解决方案1】:

    在您的config/application.rb 中,查看您的config.assets.paths 行。它应该包括“字体”。

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

    编辑

    之后,您的字体应该可以通过 font-path 助手获得:

    @font-face
      font-family: 'FontName'
      src: url(font-path('font-name.eot'))
      ...
    

    【讨论】:

    • 我得到了同样的错误。我会将我的 application.rb 添加到我的原始帖子中。
    • 只是为了确保……您在编辑该配置文件后重新启动了服务器,对吧?
    • 是的,我重启了服务器。
    【解决方案2】:

    原来我正在尝试@import "fonts",这是一个目录而不是文件。所以正如@nickcoxdotme 建议的那样,我添加了

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

    application.rb

    另外,我在 scss 文件中这样调用我的字体:

    @font-face {
      font-family: 'hamilton19';
      src: asset_url('hamilton_wood_type_foundry_-_hwtunitgothic-719-webfont.eot');
      src: asset_url('hamilton_wood_type_foundry_-_hwtunitgothic-719-webfont.eot?#iefix') format('embedded-opentype'),
        asset_url(hamilton_wood_type_foundry_-_hwtunitgothic-719-webfont.woff') format('woff'),
        asset_url('hamilton_wood_type_foundry_-_hwtunitgothic-719-webfont.ttf') format('truetype'),
        asset_url('hamilton_wood_type_foundry_-_hwtunitgothic-719-webfont.svg#MonoSocialIconsFont') format('svg');
      src: asset_url('hamilton_wood_type_foundry_-_hwtunitgothic-719-webfont.ttf') format('truetype');
      font-weight: normal;
      font-style: normal;
    }
    

    当我需要在路径中使用font_url'/assets/ 调用它们时。

    @font-face {
      font-family: 'hamilton19';
      src: font_url('/assets/hamilton_wood_type_foundry_-_hwtunitgothic-719-webfont.eot');
      src: font_url('/assets/hamilton_wood_type_foundry_-_hwtunitgothic-719-webfont.eot?#iefix') format('embedded-opentype'),
         font_url('/assets/hamilton_wood_type_foundry_-_hwtunitgothic-719-webfont.woff') format('woff'),
         font_url('/assets/hamilton_wood_type_foundry_-_hwtunitgothic-719-webfont.ttf') format('truetype'),
         font_url('/assets/hamilton_wood_type_foundry_-_hwtunitgothic-719-webfont.svg#MonoSocialIconsFont') format('svg');
      src: font_url('/assets/hamilton_wood_type_foundry_-_hwtunitgothic-719-webfont.ttf') format('truetype');
      font-weight: normal;
      font-style: normal;
    }
    

    【讨论】:

    • 你会喜欢这个的:你可以使用 font-path 帮助器来摆脱所有这些讨厌的路径。查看我的编辑。
    猜你喜欢
    • 1970-01-01
    • 2013-11-11
    • 2020-04-26
    • 2019-11-20
    • 1970-01-01
    • 1970-01-01
    • 2012-03-19
    • 2020-03-01
    • 2020-01-12
    相关资源
    最近更新 更多