在Ruby on Rails 3.2应用程序中集成googlefonts

时间:2012-12-09 17:34:25

标签: ruby-on-rails ruby-on-rails-3 asset-pipeline google-font-api

我必须在一个Rails应用程序中使用Google的字体。普通的HTML代码定义了googlefonts用法,如下所示:

<link href='http://fonts.googleapis.com/css?family=PT+Sans:400,700,400italic' rel='stylesheet' type='text/css'>

我尝试在alpplication.html.erb中添加相同的行,但它没有用。然后我将@ font-face定义(从link-href指向的位置)解压缩到一个单独的css.scss文件中,并将该文件包含在我的manifest css文件中:

#googlefonts.css.scss
@font-face {
font-family: 'PT Sans';
font-style: italic;
font-weight: 400;
src: local('PT Sans Italic'), local('PTSans-Italic'), url(http://themes.googleusercontent.com/static/fonts/ptsans/v4/xxxx.woff) format('woff');

}

#application.css

   *= require googlefonts
   *= require_tree .

它没有再次发生。

请帮我解决这个难题。在Rails 3应用程序中使用Google托管的字体的最佳做法是什么?

编辑:这是我的css,指的是@ font-face定义:

#styles.css.scss
body{
font-family: "PT Sans", Arial, Helvetica, sans-serif;
font-size:13px;
line-height:25px;
text-shadow:none !important;
color:#444;
}

2 个答案:

答案 0 :(得分:4)

使用您的示例代码,以下对我来说很合适:

# stick this inside the head section of application.html.erb 
<link href='http://fonts.googleapis.com/css?family=PT+Sans:400,700,400italic' rel='stylesheet' type='text/css'>

# stick this in your css file:
body{
  font-family: "PT Sans", Arial, Helvetica, sans-serif;
}

这就是你所需要的,它运作得很好。我建议放弃googlefonts.css.scss文件,除非你有你想要提供的字体的本地副本。链接谷歌为您提供实际处理该代码的任何方式,see for yourself

答案 1 :(得分:1)

我想我发现了这个问题:

炼油厂部分位于炼油厂/厂房。此部分的格式为.erb(默认值)。我的application.html是.haml格式。当我将_head.thml.erb转换为_head.html.haml时,它工作得很好。设置谷歌字体很简单,就像Stephenmurdoch在他的回答中所描述的那样。