无法在Bootstrap中加载资源

时间:2016-03-10 01:35:48

标签: twitter-bootstrap ruby-on-rails-4 glyphicons

我使用 Bootstrap-sass(3.3.5,3.3.1.0,3.2.0.2)

我的字体文件保存在以下路径中:

  

应用程序/资产/字体/自举

glyphicons-halflings-regular.eot
glyphicons-halflings-regular.svg
glyphicons-halflings-regular.ttf
glyphicons-halflings-regular.woff

当我渲染页面时,我收到此错误:

Started GET "/fonts/glyphicons-halflings-regular.ttf" for 127.0.0.1 at 2016-03-09 14:50:55 -0600

ActionController::RoutingError (No route matches [GET] "/fonts/glyphicons-halflings-regular.ttf"):
  actionpack (4.1.7) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
  actionpack (4.1.7) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
  railties (4.1.7) lib/rails/rack/logger.rb:38:in `call_app'
  railties (4.1.7) lib/rails/rack/logger.rb:20:in `block in call'
  activesupport (4.1.7) lib/active_support/tagged_logging.rb:68:in `block in tagged'
  activesupport (4.1.7) lib/active_support/tagged_logging.rb:26:in `tagged'
  activesupport (4.1.7) lib/active_support/tagged_logging.rb:68:in `tagged'
  railties (4.1.7) lib/rails/rack/logger.rb:20:in `call'
  actionpack (4.1.7) lib/action_dispatch/middleware/request_id.rb:21:in `call'
  rack (1.5.5) lib/rack/methodoverride.rb:21:in `call'
  rack (1.5.5) lib/rack/runtime.rb:17:in `call'
  activesupport (4.1.7) lib/active_support/cache/strategy/local_cache_middleware.rb:26:in `call'
  rack (1.5.5) lib/rack/lock.rb:17:in `call'
  actionpack (4.1.7) lib/action_dispatch/middleware/static.rb:84:in `call'
  rack (1.5.5) lib/rack/sendfile.rb:112:in `call'
  railties (4.1.7) lib/rails/engine.rb:514:in `call'
  railties (4.1.7) lib/rails/application.rb:144:in `call'
  rack (1.5.5) lib/rack/lock.rb:17:in `call'
  rack (1.5.5) lib/rack/content_length.rb:14:in `call'
  rack (1.5.5) lib/rack/handler/webrick.rb:60:in `service'
  /Users/andreucasadella/.rvm/rubies/ruby-2.1.4/lib/ruby/2.1.0/webrick/httpserver.rb:138:in `service'
  /Users/andreucasadella/.rvm/rubies/ruby-2.1.4/lib/ruby/2.1.0/webrick/httpserver.rb:94:in `run'
  /Users/andreucasadella/.rvm/rubies/ruby-2.1.4/lib/ruby/2.1.0/webrick/server.rb:295:in `block in start_thread'


  Rendered /Users/andreucasadella/.rvm/gems/ruby-2.1.4/gems/actionpack-4.1.7/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (4.0ms)
  Rendered /Users/andreucasadella/.rvm/gems/ruby-2.1.4/gems/actionpack-4.1.7/lib/action_dispatch/middleware/templates/routes/_route.html.erb (22.7ms)
  Rendered /Users/andreucasadella/.rvm/gems/ruby-2.1.4/gems/actionpack-4.1.7/lib/action_dispatch/middleware/templates/routes/_table.html.erb (4.6ms)
  Rendered /Users/andreucasadella/.rvm/gems/ruby-2.1.4/gems/actionpack-4.1.7/lib/action_dispatch/middleware/templates/rescues/routing_error.html.erb within rescues/layout (146.0ms)

我也尝试过将它们移到:

  

应用程序/资产/字体

但仍然失败。

2 个答案:

答案 0 :(得分:1)

我在公共目录中添加了一个fonts文件夹:

public/fonts/glyphicons-halflings-regular.eot
public/fonts/glyphicons-halflings-regular.svg
public/fonts/glyphicons-halflings-regular.ttf
public/fonts/glyphicons-halflings-regular.woff

这解决了这个问题。

答案 1 :(得分:0)

您是否在config/application.rb中配置资产路径以加载app/assets/fonts目录?

如果没有,你可以添加这样的东西:

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

完成后,您还应该在sass / less / css /来自公共编译资产目录的任何文件中加载字体。例如,使用帮助器asset-url

可以这样做
@font-face {
  font-family: 'MyFont';
  src: asset-url('myfont.eot');
  src: asset-url('myfont.eot?#iefix') format('embedded-opentype'),
       asset-url('myfont.woff2') format('woff2'),
  // etc ...
}

您可以在此处详细了解资产的运作方式:http://guides.rubyonrails.org/asset_pipeline.html