我在 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