Bootstrap 3图标在Heroku上无法显示

5

我在 Stack Overflow 上看到很多关于这个问题的问答和解决方案,但是没有一个对我有效。Glyphicons 在本地工作得很好,但是在 Heroku 上从桌面浏览器中显示为方块。

我是这样使用它们的:

<span class="glyphicon glyphicon-trash"></span>

这是我的Gemfile文件:
source 'https://rubygems.org'
ruby '2.0.0'

gem 'rails', '4.1.8'
gem 'sass-rails', '~> 4.0.0'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.0.0'
gem 'jquery-rails', '~> 3.1.0'
gem 'turbolinks'
gem 'jquery-turbolinks'
gem 'jbuilder', '~> 2.0.2'
gem 'bootstrap-sass', '~> 3.1.1.0'
gem 'twitter-bootstrap-rails', '~> 3.2.0'
gem 'devise', '~> 3.2.2'
gem 'simple_form'
gem "paperclip", "~> 4.2.0"
gem 'aws-sdk', '~> 1.32.0'
gem 'will_paginate', '3.0.7'
gem 'will_paginate-bootstrap'
gem "friendly_id", "~> 5.0.2"
gem 'faker', '1.2.0'
gem 'auto_html'
gem 'protected_attributes'
gem 'ckeditor', '~> 4.0.11'
gem 'font-awesome-rails'

# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'

group :production do
    gem 'pg'
    gem 'rails_12factor'
end

group :development, :test do    
  gem 'sqlite3'
end

我尝试了其他方法后,目前将production.rb设置为以下内容:

config.serve_static_assets = true
config.assets.compress = true
config.assets.compile = true
config.assets.digest = true

我的application.rb文件如下:

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

# Enable the asset pipeline
config.assets.enabled = true

# Version of your assets, change this if you want to expire all your assets
config.assets.version = '1.0'

# Required for Devise on Heroku
config.assets.initialize_on_precompile = false

config.autoload_paths += %W(#{config.root}/app/models/ckeditor)
config.assets.precompile += Ckeditor.assets
config.assets.precompile += %w(ckeditor/*)
config.assets.precompile += %w( .svg .eot .woff .ttf )
config.assets.precompile += [ 'styles.css.scss']
config.assets.precompile += [
    'glyphicons-halflings.png',
    'glyphicons-halflings-white.png'
  ]

我也尝试了运行。
bundle exec rake assets:precompile

并且

bundle exec rake assets:precompile RAILS_ENV=production

我尝试了许多修复方法,现在可能会有一些设置出了问题。本地一切正常。我还遇到一个问题,即CSS文件中的text-align:center未被推送到Heroku。我将它作为单独的问题发布,但我提到它是因为它可能与资产管道有关。以下是我在应用程序中加载样式表的方式:
 <%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => true %>

这些是我的样式表:

application.css.scss
pages.css.scss
posts.css.scss
scaffolds.css.scss
styles.css.scss
users.css.scss

在我的 application.css.scss 文件中:
*= require_self
*= require_tree .
*= require font-awesome

在我的styles.css.scss文件中。
@import 'bootstrap';
@import url("//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap-glyphicons.css");
@import 'font-awesome';


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

以下是Chrome浏览器的错误信息:
来自 'http://thetens.herokuapp.com' 的字体因跨域资源共享政策而被阻止加载:所请求的资源上没有'Access-Control-Allow-Origin'标头。因此,不允许从 'http://www.thetens.us' 访问。响应的HTTP状态代码为404。 jquery-91eecca5c0fff73b540ae82e1b1833c8.js:3581 GET http://thetens.herokuapp.com/fonts/glyphicons-halflings-regular.woff jquery-91eecca5c0fff73b540ae82e1b1833c8.js:3581 (anonymous function)jquery-91eecca5c0fff73b540ae82e1b1833c8.js:3120 firejquery-91eecca5c0fff73b540ae82e1b1833c8.js:3232 self.fireWithjquery-91eecca5c0fff73b540ae82e1b1833c8.js:3444 jQuery.extend.readyjquery-91eecca5c0fff73b540ae82e1b1833c8.js:3475 completed (index):1 来自 'http://thetens.herokuapp.com' 的字体因跨域资源共享政策而被阻止加载:所请求的资源上没有'Access-Control-Allow-Origin'标头。因此,不允许从 'http://www.thetens.us' 访问。响应的HTTP状态代码为404。 (index):1 GET http://thetens.herokuapp.com/fonts/glyphicons-halflings-regular.ttf
更新:我已将其注释掉。
# config.action_controller.asset_host = "http://thetens.herokuapp.com"

现在我只收到两个错误:

GET http://www.thetens.us/fonts/glyphicons-halflings-regular.woff jquery-6156cfcaa88efb694ac4fac3d8cef9c4.js:3581 (匿名函数)jquery-6156cfcaa88efb694ac4fac3d8cef9c4.js:3120 firejquery-6156cfcaa88efb694ac4fac3d8cef9c4.js:3232 self.fireWithjquery-6156cfcaa88efb694ac4fac3d8cef9c4.js:3444 jQuery.extend.readyjquery-6156cfcaa88efb694ac4fac3d8cef9c4.js:3475 completed (index):1 GET http://www.thetens.us/fonts/glyphicons-halflings-regular.ttf


查看样式表,确保绝对路径正确链接。 - MZaragoza
是的,请确保在 CSS 文件中调用字体时以 "/assets/..." 开头。 - MZaragoza
你能发布一下 *= require font-awesome 的内容吗? - MZaragoza
@MZaragoza 这是font-awesome-rails宝石。当字形图标无法使用时,我尝试将其用作替代品。问题在那之前就开始了,但如果需要的话,我可能可以删除它们。我还添加了一些额外的样式表信息。 - Tony Tambe
我最终只使用了font-awesome。无论如何,我很想弄清楚这个问题,因为我相信其他人也会受益。我会继续努力解决它。 - Tony Tambe
显示剩余2条评论
3个回答

1

对我而言,这个方法行得通:

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

希望这能对你有所帮助!:)


0

你必须绝对链接到字体,例如

  @import 'bootstrap';
  @import url("//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap-glyphicons.css");
  @import 'font-awesome';


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

确保在Heroku中正确链接字体


你粘贴的答案不就是我在问题中提到的代码吗?如果我漏看了什么,对不起。 - Tony Tambe
仔细看字体路径,它以“/assets”开头,这必须是从加载字体的绝对路径。 - MZaragoza
我必须询问字体从您的应用程序根目录所在的路径是什么。 - MZaragoza
1
它们在/vendor/assets/fonts目录下。我尝试将上面的路径更改为'/assets/fonts/'和'/vendor/assets/fonts/',但都没有起作用。 - Tony Tambe
让我们在聊天中继续这个讨论 - Tony Tambe
显示剩余4条评论

0

在Heroku上遇到了同样的问题,这是由于跨域资源共享引起的。这就是为什么大多数配置解决方案都不起作用的原因。

我的解决方案是:

  1. 将所有资产移动到S3存储桶中(使用asset_sync gem
  2. 在存储桶上配置CORS(CORS)[重要]

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