【问题标题】:Rails 4.1 and Bootstrap 3 glyphicons are not workingRails 4.1 和 Bootstrap 3 字形图标不起作用
【发布时间】:2014-07-17 19:50:44
【问题描述】:

我正在尝试消除使用 Bootstrap 3 的 Rails 4 项目中的字形图标错误。我没有使用任何 Bootstrap gem 将其添加到资产管道中。我手动将 bootstrap.css 和 bootstrap.js 添加到它们各自的 app/assets 目录中,并将它们添加到 application.css 和 application.js 我现在在我的 Web 浏览器的控制台中看到以下内容:

GET http://localhost:3000/fonts/glyphicons-halflings-regular.woff 404 (Not Found) localhost/:1
GET http://localhost:3000/fonts/glyphicons-halflings-regular.ttf 404 (Not Found) localhost/:1
GET http://localhost:3000/fonts/glyphicons-halflings-regular.svg 404 (Not Found) 

如何在 Ruby on Rails 应用程序中解决此问题?我尝试将上述文件复制到app/assets/fonts 并将其弹出到application.rb:

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

运气不好。

【问题讨论】:

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


    【解决方案1】:

    将 woff、ttf 和 svg 文件添加到:

    RAILS_ROOT/vendor/assets/fonts
    

    如果它不存在,则创建它。它们应该存在于您下载的引导程序构建中。

    此外,您需要在所有 require 语句之后将其添加到 application.css:

    @font-face {
        font-family: 'Glyphicons Halflings';
        src: url('../assets/glyphicons-halflings-regular.eot');
        src: url('../assets/glyphicons-halflings-regular.eot?#iefix') format('embedded-opentype'), url('../assets/glyphicons-halflings-regular.woff') format('woff'), url('../assets/glyphicons-halflings-regular.ttf') format('truetype'), url('../assets/glyphicons-halflings-regular.svg#glyphicons_halflingsregular') format('svg');
    }
    

    【讨论】:

    • 有什么理由将它们放在供应商而不是通常的app/assets/ 目录中?
    • vendor/* 资产仅在 application.css/js 文件中明确提及时才包含在内。这可能是你应该如何处理任何你不是自己写的东西。在我的项目中,引导程序始终位于 vendor/* 树中。
    • 看来我可以让它工作的唯一方法是将字体复制到 public/fonts 目录,不确定这是否是正确的方法。
    • 它可能无法比这种方式更“正确” - github.com/twbs/bootstrap-sass - 这是官方推荐的在 Rails 应用程序中包含引导程序的方式。
    • 最后我按照@randombits 建议的思路破解了这个,将字体文件直接复制到public/assets/fonts。我不喜欢它,但在阅读了十几篇博客文章和多个 StackOverflow 答案之后,这是唯一有效的方法。
    【解决方案2】:

    为了让字形图标正常工作,我必须在 config/application.rb 文件中添加一行。在类 Application 中添加以下内容

    config.assets.paths << "#{Rails}/vendor/assets/fonts"
    

    然后在终端运行命令以获取要编译的资产:

    rake assets:precompile RAILS_ENV=development
    

    现在我们需要更新 bootrap.css 文件(您可能也需要更新 bootstrap.min.css 文件),使用文本编辑器搜索 @font-face 并将路径更新为包含 /assets/glyphicons-halfings-regular.* 的字体 URL(包括文件扩展名)。

    这是 url 最初在 bootstrap.css 文件中的内容。

    @font-face {
        font-family: 'Glyphicons Halflings';
        src: url('../fonts/glyphicons-halflings-regular.eot');
        src: url('../fonts/glyphicons-halflings-regular.eot?#iefix') format('embedded-opentype'), url('../fonts/glyphicons-halflings-regular.woff') format('woff'), url('../fonts/glyphicons-halflings-regular.ttf') format('truetype'), url('../fonts/glyphicons-halflings-regular.svg#glyphicons-halflingsregular') format('svg');
    }
    

    您希望将每个资源位置更改为 /assets/glyphicons-halfings-regular.*,如下所示。

    @font-face {
        font-family: 'Glyphicons Halflings';
        src: url('/assets/glyphicons-halflings-regular.eot');
        src: url('/assets/glyphicons-halflings-regular.eot?#iefix') format('embedded-opentype'), url('/assets/glyphicons-halflings-regular.woff') format('woff'), url('/assets/glyphicons-halflings-regular.ttf') format('truetype'), url('/assets/glyphicons-halflings-regular.svg#glyphicons-halflingsregular') format('svg');
    }
    

    来源:[Erik Minkel Blog]

    【讨论】:

    • 我避免在开发中预编译资产。 Rails 将为您缓存的预编译资产提供服务,而不是您尝试开发的更改……这会让人头疼。
    【解决方案3】:

    上面提供的所有解决方案都是肮脏的黑客。解决此问题的正确方法是在您的 sass 文件中引导之前包含“bootstrap-sprockets”:

    @import "bootstrap-sprockets";
    @import "bootstrap";
    

    【讨论】:

      【解决方案4】:

      将字体移动到 /app/public 文件夹有效,但看起来不像 Rails 方式。

      对我有用的是将它们放入 /app/assets/fonts 并在 bootstrap.css 中注释掉:

      /*
      @font-face {
          font-family: 'Glyphicons Halflings';
          src: url('../fonts/glyphicons-halflings-regular.eot');
          src: url('../fonts/glyphicons-halflings-regular.eot?#iefix') format('embedded-opentype'), url('../fonts/glyphicons-halflings-regular.woff2') format('woff2'), url('../fonts/glyphicons-halflings-regular.woff') format('woff'), url('../fonts/glyphicons-halflings-regular.ttf') format('truetype'), url('../fonts/glyphicons-halflings-regular.svg#glyphicons_halflingsregular') format('svg');
      }
      */
      

      【讨论】:

        【解决方案5】:

        基本上你应该只导入引导程序

        @import "bootstrap-sprockets";
        @import "bootstrap";
        

        在@import "bootstrap-sprockets"; 之前导入引导组件时出现问题 覆盖应该在@import "bootstrap-sprockets";之后进行

        // bootstrap-sprockets goes first
        @import "bootstrap-sprockets";
        
        // Overrides
        @import "bootstrap/variables"; // it uses $bootstrap-sass-asset-helper which is defined in bootstrap-sprockets
        $btn-primary-bg: $gray-light;
        
        @import "bootstrap";
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-02-24
          • 1970-01-01
          • 2015-07-11
          • 2013-11-26
          • 1970-01-01
          • 2015-01-15
          • 2016-04-13
          • 2014-01-30
          相关资源
          最近更新 更多