简体   繁体   English

Rails 4.1和Bootstrap 3 glyphicons无法正常工作

[英]Rails 4.1 and Bootstrap 3 glyphicons are not working

I am trying to get rid of the glyphicon errors in my Rails 4 project that's using Bootstrap 3. I'm not using any Bootstrap gems to add it to the asset pipeline. 我试图摆脱使用Bootstrap 3的Rails 4项目中的glyphicon错误。我没有使用任何Bootstrap gems将其添加到资产管道。 I manually added bootstrap.css and bootstrap.js to their respective app/assets directories and added them to application.css and application.js What I'm seeing now is the following in my web browser's console: 我手动将bootstrap.css和bootstrap.js添加到各自的app/assets目录,并将它们添加到application.cssapplication.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) 

What can be done to fix this in a Ruby on Rails application? 在Ruby on Rails应用程序中可以做些什么来解决这个问题? I tried copying said files to app/assets/fonts and popped this into application.rb : 我尝试将所述文件复制到app/assets/fonts并将其弹出到application.rb

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

No luck. 没运气。

All solutions provided above are dirty hacks. 上面提供的所有解决方案都是脏的。 The correct way to solve this issue is to include "bootstrap-sprockets" before bootstrap in your sass files: 解决此问题的正确方法是在sass文件中的bootstrap之前包含“bootstrap-sprockets”:

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

To get the glyphicons working I had to add a line to the config/application.rb file. 为了让glyphicons工作,我必须在config / application.rb文件中添加一行。 Add the following within class Application < Rails::Application. 在Application <Rails :: Application类中添加以下内容。

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

Then at the terminal run the command to get the assets to compile: 然后在终端运行命令以获取要编译的资产:

rake assets:precompile RAILS_ENV=development

Now we need to update the bootrap.css file (you'll likely need to update the bootstrap.min.css file as a result, too), search for @font-face with your text editor and update the paths to the font urls to include /assets/glyphicons-halfings-regular.* (include the file extension). 现在我们需要更新bootrap.css文件(您可能也需要更新bootstrap.min.css文件),使用文本编辑器搜索@ font-face并更新字体URL的路径包括/assets/glyphicons-halfings-regular.*(包括文件扩展名)。

This is what the url's will be originally in the bootstrap.css file. 这是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');
}

You want to change each of these resource locations to /assets/glyphicons-halfings-regular.* as shown below. 您希望将每个资源位置更改为/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');
}

SOURCE: [Erik Minkel Blog] 消息来源: [Erik Minkel博客]

Basically you should just import bootstrap 基本上你应该只导入bootstrap

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

Problems appear when you import bootstrap components before @import "bootstrap-sprockets"; @import "bootstrap-sprockets";之前导入引导程序组件时出现问题@import "bootstrap-sprockets"; Overrides should go after @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";

Add the woff, ttf and svg files to: 将woff,ttf和svg文件添加到:

RAILS_ROOT/vendor/assets/fonts

Create this if it doesn't exist. 如果它不存在,请创建它。 They should be present in the build of bootstrap you downloaded. 它们应该出现在您下载的bootstrap构建中。

Also, you'll need to add this to your application.css after all your require statements: 此外,您需要在所有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');
}

Moving the fonts to the /app/public folder worked but didn't seem like the Rails way. 将字体移动到/ app / public文件夹有效,但看起来不像Rails方式。

What worked for me was putting them in /app/assets/fonts and commenting this out in bootstrap.css: 对我来说有用的是把它们放在/ 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');
}
*/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM