Bootstrap 3 Glyphicon错误

5

我知道这里已经有类似的问题得到了解答,但是很多答案都使用了gem或直接修改Bootstrap文件。虽然这些是有效的配置,但我想在这个领域(Sprockets、Rails Pipeline、前端开发)中增加我的知识,并想了解为什么这不起作用,背后的问题是什么。

Ruby 2.0.0-p353,Rails 4.0.2,Bootstrap 3.0.3,Nginx 1.4.4,Passenger 4.0.33

我从Bootstrap网站下载了最新的zip文件,执行了所有操作(点击此处)将这些文件直接包含在我的应用程序中,在我的开发环境中能够如预期那样完全显示出glyhpicons。然而,当我部署到生产环境时,在Chrome浏览器中glyhpicons会显示为方块。我在生产预编译中添加了glyhpicons文件的各个文件扩展名,并验证它们出现在我的public/assets文件夹中。js/css文件和glyphicon文件之间唯一的差别是glyphicon文件旁边没有gzipped文件。

production.rb:

config.assets.precompile = ['*.js', '*.css', '*.eot', '*.svg', '*.ttf', '*.woff']

我知道Rails在开发和生产环境下以不同的方式加载资源,但为什么生产环境中它没有看到或理解glyphicon资源呢?是否有办法通过当前配置来修复这个问题,还是我需要改变包含这些资源的方式,以便能够修复这个错误?
此外,在开发和生产环境下,每当我渲染一个使用glyphicon的页面时,我都会得到以下错误,但图标仍然显示出来。这是否相关?
- ActionController :: RoutingError(无法匹配[GET]“/fonts/glyphicons-halflings-regular.woff”) - ActionController :: RoutingError(无法匹配[GET]“/fonts/glyphicons-halflings-regular.ttf”) - ActionController :: RoutingError(无法匹配[GET]“/fonts/glyphicons-halflings-regular.svg”)

+1 无法正常工作。 - Tim Kretschmer
1
@import "bootstrap"之前加上@import "bootstrap-sprockets"帮助了我,尽管我不知道问题的原因。 - Konstantine Kalbazov
2个回答

3
你需要手动从网站上下载 glyphicons 字体,并将其放在你的 app/assets/stylesheets/fonts 文件夹中。
目前 bootstrap gem 还没有解决一些问题。
你可以参考下面的链接: Bootstrap 3 Glyphicons are not working

0
config/application.rb中,在class Application < Rails::Application之后添加以下内容。它应该看起来像这样:
class Application < Rails::Application
    config.assets.paths << "#{Rails}/vendor/assets/fonts"

在终端中运行以下命令编译您的资产:
rake assets:precompile RAILS_ENV=development

通过将@font-face资源位置从../fonts/更改为/assets/,编辑bootstrap.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');
}

完成了。只需使用rails s重新启动,图标就会出现。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接