简体   繁体   English

Glyphicon在本地工作但不在Heroku上工作

[英]Glyphicon works locally but not on Heroku

If you look at my app here: http://quiet-brushlands-5712.herokuapp.com/ , close to the button is an icon, a glyphicon. 如果你在这里查看我的应用程序: http//quiet-brushlands-5712.herokuapp.com/ ,靠近按钮的是一个图标,一个glyphicon。 But it's not appearing on heroku. 但它没有出现在heroku上。 locally it show as nicely like this: 在本地它显示如下:

在此输入图像描述

I tried the "solution" from here Using boostrap.css file in Rails production/heroku w/o LESS or SASS and from other links in Google. 我从这里尝试了“解决方案” 使用Rails生产中的boostrap.css文件/没有LESS或SASS以及Google中的其他链接。 I need the glyph to show on Heroku as well. 我也需要字形在Heroku上显示。 Any help? 有帮助吗?

I didn't download the Twitter Bootstrap manually, I'm just using a gem. 我没有手动下载Twitter Bootstrap,我只是使用了一个gem。 In my Javascript console I have this: 在我的Javascript控制台中,我有这个:

GET http://quiet-brushlands-5712.herokuapp.com/assets/glyphicons-halflings.png 404 (Not Found)

My Gemfile if it helps: 我的Gemfile如果有帮助:

source 'https://rubygems.org'

gem 'rails', '4.0.2'
gem 'sass-rails', '~> 4.0.0'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.0.0'
gem 'jquery-rails', '3.0.4'
gem 'turbolinks', '2.0.0'
gem 'jbuilder', '~> 1.2'
gem 'bootstrap-sass', '2.1'
gem 'jquery-ui-rails', '4.1.0'
gem 'pg'
gem 'font-awesome-rails', '4.0.3.0'

group :doc do
  # bundle exec rake doc:rails generates the API under doc/api.
  gem 'sdoc', require: false
end

group :development, :test do
  gem 'rspec-rails', '2.11.0'
  gem 'guard-rspec', '1.2.1'
  gem 'guard-spork', '1.2.0'
  gem 'childprocess', '0.3.6'
  gem 'spork', '0.9.2'
end

group :production do
  gem 'rails_12factor'
end

解决方案是在config/environments/production.rb文件中将config.assets.compile = false更改为config.assets.compile = true

Solution that not use the compilation fallback (tested on Rails 4.1): In your scss files, import bootstrap-sprockets just before bootstrap. 不使用编译回退的解决方案(在Rails 4.1上测试):在scss文件中,在bootstrap之前导入bootstrap-sprockets。

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

Further to Vidya's comment, it's probably going to be your asset pipeline that's the problem 继Vidya的评论之后,这可能是你的资产管道问题

Although I'm not familiar with the specifics of Bootstrap for this, there is a major issue with Heroku's asset pipeline, being that it has to be precompiled before you can use it effectively 虽然我不熟悉Bootstrap的具体细节,但Heroku的资产管道存在一个主要问题,即必须先进行预编译才能有效地使用它。


Asset Fingerprinting 资产指纹识别

Heroku requires you to precompile your assets because of asset fingerprinting 由于资产指纹识别, Heroku要求您预编译资产

This is where your assets will have a hash applied to the end of their filename, like image-12sdafdsafkj223423jnjfadsnfsad.png or similar. 这是您的资产将在其文件名末尾应用哈希的位置,如image-12sdafdsafkj223423jnjfadsnfsad.png或类似名称。 The reason for this is apparently to keep the assets unique or something 这样做的原因显然是为了保持资产的独特性

If you follow the link provided by Vidya , you'll find that Heroku prompts to you precompile your assets using the Rails CMD. 如果您按照Vidya提供的链接 ,您会发现Heroku会提示您使用Rails CMD预编译您的资产。 What it doesn't tell you is that this will mess up your images unless they've been dynamically-assigned 它没有告诉你的是,除非动态分配,否则这会弄乱你的图像


SCSS SCSS

As mentioned, I'm not sure about how this applies to Bootstrap specifically, but with Heroku's asset pipeline stuff, you need to ensure your images are assigned using dynamic paths 如上所述,我不确定具体如何适用于Bootstrap,但是使用Heroku的资产管道,您需要确保使用动态路径分配图像

Like in Rails itself, SCSS allows you to use asset_path or image_path to create a dynamic link. 与Rails本身一样,SCSS允许您使用asset_pathimage_path来创建动态链接。 This is what you have to do to fix your problem. 这是您必须要解决的问题。 Here's some code we use, which works on Heroku: 这是我们使用的一些代码,适用于Heroku:

.navigation_bar {
        z-index: 200;
        position: relative;
        background: asset_url('nav_bar/bg.png') repeat-x top;
}

如果您不希望在生产环境中将config.assets.compile设置为true(您可能不应该为了更好的性能),则可以在使用rake assets:precompile RAILS_ENV=production推送到heroku之前手动预编译rake assets:precompile RAILS_ENV=production

In config/environments/production.rb config/environments/production.rb

Change config.assets.compile = false to config.assets.compile = true config.assets.compile = false更改为config.assets.compile = true

(and then don't forget to commit and push to heroku) (然后不要忘记提交并推送到heroku)

Worked for me... 为我工作......

Because I came to this answer when I was searching I figure I should put here that the solution I came across was to include an import to "bootstrap-sprockets" before bootstrap. 因为我在搜索时得到了这个答案,我想我应该在这里说我遇到的解决方案是在bootstrap之前包含一个导入到“bootstrap-sprockets”。 This is documented at https://github.com/twbs/bootstrap-sass/issues/653 . 这在https://github.com/twbs/bootstrap-sass/issues/653中有记录。

I realize the question isn't using bootstrap-sass 3.2+ but I figure this may help someone. 我意识到问题不是使用bootstrap-sass 3.2+,但我认为这可能有助于某人。

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

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