【问题标题】:Failed to load resource in BootstrapBootstrap 加载资源失败
【发布时间】:2016-06-24 16:17:59
【问题描述】:

我正在使用 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)

我也尝试将它们移至:

应用程序/资产/字体

但仍然失败。

【问题讨论】:

  • 尝试用这种语法帮助他们 => "assets/glyphicons-halflings-regular.ttf"

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


【解决方案1】:

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

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

这解决了这个问题。

【讨论】:

    【解决方案2】:

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

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

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

    完成后,您还应该将字体加载到公共编译资产目录中的 sass/less/css/whatever 文件中。以 sass 为例,您可以使用 helper 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

    【讨论】:

    • 嗨 Na0na0,我尝试了您的建议,但没有奏效。我刚刚从您的链接参考中读到:资产管道在技术上不再是 Rails 4 的核心功能,它已从框架中提取到 sprockets-rails gem 中。
    • 它还说它默认启用,并且由于您使用的是 bootstrap-sass,我不知道您不会使用它,因为它依赖于 bootstrap-sass gem: github.com/twbs/bootstrap-sass/blob/master/… 除非您使用 gulp/bower/npm/... 并在创建应用程序时使用 --skip-sprockets ?如果是这样,请正确标记您的问题,以便具有正确知识的人可以帮助您。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-24
    • 2018-01-15
    • 2013-02-26
    • 2019-05-28
    • 2016-07-01
    • 2011-06-21
    • 1970-01-01
    相关资源
    最近更新 更多