红宝石不加载字体真棒 [英] ruby not load font-awesome

查看:60
本文介绍了红宝石不加载字体真棒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将超棒的字体加载到我的视图中

i am trying to load font-awesome to my view

我还阅读了其他答案,并实现了 Rails:Using Font Awesome

I have read other answers and implement the Rails: Using Font Awesome

我做了什么

config/application.rb

config/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(:default, Rails.env)

    module Blog
      class Application < Rails::Application
        # Settings in config/environments/* take precedence over those specified here.
        # Application configuration should go into files in config/initializers
        # -- all .rb files in that directory are automatically loaded.

        # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
        # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
        # config.time_zone = 'Central Time (US & Canada)'

        # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
        # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
        # config.i18n.default_locale = :de

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

我已将font-awesome.min.css重命名为font-awesome.min.css.erb并使用assets_path

I have rename the font-awesome.min.css to font-awesome.min.css.erb and use assets_path

@font-face{font-family:'FontAwesome';
 src:url('<%= asset_path("fontawesome-webfont.eot?v=4.0.3") %>');
 src:url('<%= asset_path("awesome-webfont.eot?#iefix&v=4.0.3") %>') format('embedded-opentype'),
 url('<%= asset_path("fontawesome-webfont.woff?v=4.0.3") %>') format('woff'),
 url('<%= asset_path("fontawesome-webfont.ttf?v=4.0.3") %>') format('truetype'),
 url('<%= asset_path("fontawesome-webfont.svg?v=4.0.3#fontawesomeregula") %>') format('svg');
 font-weight:normal;font-style:normal}

并在视图中加载css文件

and load the css file on the view

<%= stylesheet_link_tag 'bootstrap.css' %>
  <%= stylesheet_link_tag 'animate.css' %>
  <%= stylesheet_link_tag 'font-awesome.min.css' %>
  <%= stylesheet_link_tag 'font.css' %>
  <%= stylesheet_link_tag 'app.css' %>

并且在Chrome上未加载字体文件

and on Chrome the font files are not loaded

在服务器上我得到

Started GET "/fontawesome-webfont.ttf?v=4.0.3" for 127.0.0.1 at 2014-02-14 08:36:37 +0200

ActionController::RoutingError (No route matches [GET] "/fontawesome-webfont.ttf"):
  actionpack (4.0.2) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
  actionpack (4.0.2) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
  railties (4.0.2) lib/rails/rack/logger.rb:38:in `call_app'
  railties (4.0.2) lib/rails/rack/logger.rb:20:in `block in call'
  activesupport (4.0.2) lib/active_support/tagged_logging.rb:67:in `block in tagged'
  activesupport (4.0.2) lib/active_support/tagged_logging.rb:25:in `tagged'
  activesupport (4.0.2) lib/active_support/tagged_logging.rb:67:in `tagged'
  railties (4.0.2) lib/rails/rack/logger.rb:20:in `call'
  actionpack (4.0.2) lib/action_dispatch/middleware/request_id.rb:21:in `call'
  rack (1.5.2) lib/rack/methodoverride.rb:21:in `call'
  rack (1.5.2) lib/rack/runtime.rb:17:in `call'
  activesupport (4.0.2) lib/active_support/cache/strategy/local_cache.rb:83:in `call'
  rack (1.5.2) lib/rack/lock.rb:17:in `call'
  actionpack (4.0.2) lib/action_dispatch/middleware/static.rb:64:in `call'
  rack (1.5.2) lib/rack/sendfile.rb:112:in `call'
  railties (4.0.2) lib/rails/engine.rb:511:in `call'
  railties (4.0.2) lib/rails/application.rb:97:in `call'
  rack (1.5.2) lib/rack/lock.rb:17:in `call'
  rack (1.5.2) lib/rack/content_length.rb:14:in `call'
  rack (1.5.2) lib/rack/handler/webrick.rb:60:in `service'
  /usr/local/rvm/rubies/ruby-2.1.0/lib/ruby/2.1.0/webrick/httpserver.rb:138:in `service'
  /usr/local/rvm/rubies/ruby-2.1.0/lib/ruby/2.1.0/webrick/httpserver.rb:94:in `run'
  /usr/local/rvm/rubies/ruby-2.1.0/lib/ruby/2.1.0/webrick/server.rb:295:in `block in start_thread'

请帮助

推荐答案

将font-awesome集成到rails的最简单方法是通过 font-awesome-rails gem.它将真棒字体集成到Rails资产管道中.

The easiest way to integrate font-awesome to rails is via font-awesome-rails gem. It will integrate font-awesome to rails asset pipeline.

在您的Gemfile中添加

In your Gemfile add

gem "font-awesome-rails" 

并运行 bundle install 以安装gem

然后在您的scss文件中使用导入它

Then in your scss file import it using

@import "font-awesome";

就是这样!现在,您可以在视图中将其用作

That's it! Now you can use it in your view as

<i class="fa fa-bell-o"></i>

如果您希望使用css而不是scss,则在您的application.css中添加require语句

If you prefer to use css instead of scss then add the require statement in your application.css

*= require font-awesome  

更新

如果您不想使用font-awesome-rails gem,请按照以下步骤操作

Update

If you don't want to use font-awesome-rails gem then follow the steps

  1. 将字体放入文件夹app/assets/fonts

  1. Place your fonts in the folder app/assets/fonts

在config/application.rb中添加以下行

In config/application.rb add the following lines

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

# Precompile additional assets
config.assets.precompile += %w( .svg .eot .woff .ttf ) 

  • 在font-awesome.css.erb之类的字体中声明您的字体

  • Declare your font in font-awesome.css.erb like

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

  • 我希望这会对您有所帮助.

    I hope this will help you.

    这篇关于红宝石不加载字体真棒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆