Rails 4.0中的@ font-face,找不到自定义字体的Sass URL

15 fonts ruby-on-rails font-face ruby-on-rails-4

在我的Rails项目中,我尝试使用自定义字体.有很多与这个问题相关的答案,就像这个没有帮助的答案,我编辑development.rb:

# Add the fonts path
config.assets.paths << Rails.root.join('app', 'assets', 'fonts')

# Precompile additional assets
config.assets.precompile += %w( .svg .eot .woff .ttf )
Run Code Online (Sandbox Code Playgroud)

它仍然显示 No route matches [GET] "/assets/chalkduster-webfont.woff"

我设置我url:

@font-face {
   font-family: 'chalkdusterregular';
    src: url('chalkduster-webfont.eot');
    src:url('chalkduster-webfont.svg#chalkdusterregular') format('svg'), 
    url('chalkduster-webfont.eot?#iefix') format('embedded-opentype'),
         url('chalkduster-webfont.woff') format('woff'),
         url('chalkduster-webfont.ttf') format('truetype');
    font-weight: normal;
    font-style: normal;

}
Run Code Online (Sandbox Code Playgroud)

我font_path('')在源代码中尝试过,font-url()但它也无法运行.:(

Nic*_*s.V 32

试试asset-url().适合我.

@font-face {
   font-family: 'chalkdusterregular';
    src: asset-url('chalkduster-webfont.eot');
    src: asset-url('chalkduster-webfont.svg#chalkdusterregular') format('svg'), 
         asset-url('chalkduster-webfont.eot?#iefix') format('embedded-opentype'),
         asset-url('chalkduster-webfont.woff') format('woff'),
         asset-url('chalkduster-webfont.ttf') format('truetype');
    font-weight: normal;
    font-style: normal;
}
Run Code Online (Sandbox Code Playgroud)

此外,我只添加字体路径并在config/environments/production.rb中预编译其他资产

# Add the fonts path
config.assets.paths << Rails.root.join('app', 'assets', 'fonts')

# Precompile additional assets
config.assets.precompile += %w( .svg .eot .woff .ttf )
Run Code Online (Sandbox Code Playgroud)

不需要添加到config/environments/development.rb,因为asset-url它有点神奇.

  • 对我来说,`font-url`不起作用(链接到/fonts/file.ext),而`asset-url`工作(链接到/assets/file.ext) (3认同)