简体   繁体   English

无法使用带有bootstrap-sass gem的Rails 4在Heroku上运行CSS

[英]Can't get CSS working on Heroku using Rails 4 with bootstrap-sass gem

I have deployed an app to Heroku with one issue I can't seem to get figured out. 我已经向Heroku部署了一个应用程序,但有一个问题似乎无法解决。 The CSS for the app via Bootstrap-sass does not load up thus I have an un-styled app. 通过Bootstrap-sass的应用程序的CSS没有加载,因此我有一个非样式的应用程序。 At the moment this is just a collection of static pages. 目前,这只是静态页面的集合。

I have followed all but one step in the README https://github.com/thomas-mcdonald/bootstrap-sass The step I can't figure out and highly likely to be my issue is as follows. 我在README https://github.com/thomas-mcdonald/bootstrap-sass中只执行了一步。我无法弄清楚的步骤很可能是我的问题如下。 Due to a change in Rails that prevents images from being compiled in vendor and lib, you'll need to add the following line to your application.rb: 由于Rails中的更改会阻止在供应商和库中编译图像,因此您需要将以下行添加到application.rb:

config.assets.precompile += %w(*.png *.jpg *.jpeg *.gif)

Since I am still very new to programming, the first issue is I have no clue where and how to add this within the application.rb file. 由于我仍然是编程的新手,第一个问题是我不知道在application.rb文件中何处以及如何添加它。 I would greatly appreciate it if someone could help show me how and where to properly add the above line of code. 如果有人能帮我告诉我如何以及在何处正确添加上述代码,我将不胜感激。

The second issue could be related to the gems I am using however when I created the app, the sass-rails gem was installed with ~> 4.0.0.beta1. 第二个问题可能与我使用的宝石有关但是当我创建应用程序时,sass-rails gem安装了〜> 4.0.0.beta1。 According to the README the version to use is 3.2. 根据README,使用的版本是3.2。 Since this also might be an issue, I have included the gem file incase anyone determines that is the underlying reason for my problem. 由于这也可能是一个问题,我已经包含了gem文件,任何人都可以确定这是我的问题的根本原因。

Thank you in advance for any help you can provide. 提前感谢您提供的任何帮助。

Edit: To add the steps I took on the first try that resulted in style working properly on my local host but not once the code was deployed to heroku. 编辑:添加我在第一次尝试时采取的步骤,导致样式在我的本地主机上正常工作,但一旦代码部署到heroku就没有。

  1. Created a new rails 4 app (gem file below) 创建了一个新的rails 4 app(下面的gem文件)
  2. Added the bootstrap-sass gem listed in the gem file below 添加了下面gem文件中列出的bootstrap-sass gem
  3. Added PG gem to my gem file in the production group and moved SQLite3 to development and test (ran bundle install --without production following steps 2 and 3) 将PG gem添加到生产组中的gem文件中,并将SQLite3移至开发和测试(运行bundle install - 在步骤2和3之后不生成)
  4. created a pages controller for a static home page 为静态主页创建了一个页面控制器
  5. Added an h1 within a hero-unit on the home page just to see if style was working 在主页上的英雄单元中添加了一个h1,以查看样式是否正常工作
  6. added a styles.css.scss file and included @import 'bootstrap'; 添加了styles.css.scss文件并包含了@import'bootstrap'; to the style sheet 到样式表
  7. Created git repository, ran my initial commit and pushed the code to git 创建了git存储库,运行我的初始提交并将代码推送到git
  8. Created heroku app and pushed the master to heroku 创建了heroku应用程序并将主人推送到heroku

On the second attempt, I added a nav bar to the home page (if that makes a difference to anyone) and followed steps 7 and 8 again but just prior to doing those steps I ran the following line of code. 在第二次尝试时,我在主页上添加了一个导航栏(如果这对任何人都有影响)并再次执行步骤7和8,但在执行这些步骤之前,我运行了以下代码行。

RAILS_ENV=production bundle exec rake assets:precompile

I still ended up with a site that had the proper style on my local host but no style was working on Heroku. 我仍然最终得到了一个在我的本地主机上具有正确风格的网站,但没有样式在Heroku上工作。 As I noted above in my original post, there is a line of code that needs to be added to the application.rb file that I did not follow due to my lack of understanding how to properly add the line of code into the file. 正如我在上一篇文章中所述,有一行代码需要添加到我没有遵循的application.rb文件中,因为我对如何正确地将代码行添加到文件中缺乏了解。

Gemfile: 的Gemfile:

source 'https://rubygems.org'

ruby "2.0.0"

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.0.0.beta1'

group :production do
gem 'pg'
end

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

# Gems used only for assets and not required
# in production environments by default.
group :assets do
gem 'sass-rails',   '~> 4.0.0.beta1'
gem 'coffee-rails', '~> 4.0.0.beta1'

gem 'bootstrap-sass', '~> 2.3.1.1'

# See https://github.com/sstephenson/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby

gem 'uglifier', '>= 1.0.3'
end

gem 'jquery-rails'

# Turbolinks makes following links in your web application faster. Read more:  https://github.com/rails/turbolinks
gem 'turbolinks'

# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 1.0.1'

# To use ActiveModel has_secure_password
# gem 'bcrypt-ruby', '~> 3.0.0'

# Use unicorn as the app server
# gem 'unicorn'

# Deploy with Capistrano
# gem 'capistrano', group: :development

# To use debugger
# gem 'debugger'

I just now (June 13 2013)got this answer from Heroku devs whose support guided me across the barriers. 我刚才(2013年6月13日)得到了Heroku开发者的回答,他们的支持引导我跨过障碍。 This is how I got my css display from localhost working in my Heroku app. 这就是我在我的Heroku应用程序中使用localhost获取我的css显示的方法。

"All you need to do is turn on asset serving in production and set the logger to stdout to get Rails4 to work on Heroku. We are currently working on smoothing out the deploy process for Rails 4 apps but for the meantime, you can just change those lines in your code and you won't need those gems." “你需要做的就是打开生产中的资产服务并将logger设置为stdout以使Rails4在Heroku上工作。我们目前正在努力平滑Rails 4应用程序的部署过程,但与此同时,你可以改变代码中的那些行,你不需要那些宝石。“ (Thanks Bret and Neil great news) (感谢布雷特和尼尔的好消息)

In /config/environments/production. 在/ config / environments / production中。 set: 组:

config.cache_classes = true
config.serve_static_files = true
config.assets.compile = true
config.assets.digest = true

I do not know about the stdout in logger so can't check that. 我不知道记录器中的stdout因此无法检查。

Do a git add . 做一个git添加。 and git commit. 和git提交。

Make sure that /config/database.yml has: 确保/config/database.yml具有:

production:
  adapter: postgresql
  encoding: unicode
  database: Your_appname_production

You will need this info for the env command below. 您将需要以下env命令的此信息。

Make sure you have gem 'pg' in production in your Gemfile Do another git commit. 确保你的Gemfile中有生产中的宝石'pg'做另一个git提交。

Run this command in a terminal in your app on one line: 在应用程序的终端中的一行中运行此命令:

env RAILS_ENV=production DATABASE_URL=postgresql://user:pass@127.0.0.1/Your_app_name_production bundle exec rake assets:precompile 2>&1

Where DATABASE_URL=postgresql is identical to your production adapter in the yml file and Your_app_name_production is specified because Heroku only seems to run production. 其中DATABASE_URL = postgresql与yml文件中的生产适配器相同,并指定了Your_app_name_production,因为Heroku似乎只运行生产。

I was advised against and did not need: 我被建议反对,不需要:

group :production do
  gem 'rails_log_stdout',           github: 'heroku/rails_log_stdout'
  gem 'rails3_serve_static_assets', github: 'heroku/rails3_serve_static_assets'
end

It errors out in bundle install and Heroku. 它在bundle install和Heroku中出错。

Don't know if this helps but I also added production to 不知道这是否有帮助,但我也增加了生产

Bundler.require(*Rails.groups(assets: %w(development test production)))

Can't remember where I saw that advice. 不记得我在哪里看到了这个建议。

HTH Arel HTH Arel

只需运行bundle exec rake assets:precompile在推送到heroku之前bundle exec rake assets:precompile

I was able to fix this issue by adding these two gems to my application 我能够通过将这两个宝石添加到我的应用程序来解决这个问题

group :production do
  gem 'rails_log_stdout',           github: 'heroku/rails_log_stdout'
  gem 'rails3_serve_static_assets', github: 'heroku/rails3_serve_static_assets'
end

Add that, run bundle install and then push to heroku. 添加它,运行bundle install然后推送到heroku。

Your styles should start loading. 你的风格应该开始加载。

First of all upgrade from Rails beta to the latest release . 首先从Rails beta升级到最新版本

Check for where you might be setting config.assets.initialize_on_precompile = false as that might make it fall back to non-sprockets assets resolution (I'm guessing you might have set it to false when reading about Rails 3.x on heroku docs). 检查你可能在哪里设置config.assets.initialize_on_precompile = false因为这可能会使它回退到非sprockets资源解析(我猜你在heroku docs上阅读Rails 3.x时可能会把它设置为false) 。

Set it back to the default true 将其设置回默认值true

ruby config.assets.initialize_on_precompile = true

Then enable user-env-compile for the app on heroku: 然后在heroku上为app启用user-env-compile

# Enable precompile support for the app
heroku labs:enable user-env-compile
# Remove precompiled assets
rm -rf public/assets/
git add -u 
git commit -m 'Remove precompiled assets'
# Now push and everything should just work from now on
git push heroku master

Adapted from this bootstrap-sass issue comment . 改编自这个bootstrap-sass问题评论

Set config.assets.compile=true in the file /config/environments/production.rb : 在文件/config/environments/production.rb设置config.assets.compile=true

config.assets.compile=true

Click here to know more about the asset pipeline. 单击此处以了解有关资产管道的更多信息。

A simple reason for this heroic problem could be mixing css file types. 这个英雄问题的一个简单原因可能是混合css文件类型。 In my own experience, this happens if you push out an assets folder that contains both .css and .scss file types. 根据我自己的经验,如果你推出一个包含.css和.scss文件类型的资产文件夹,就会发生这种情况。 Maybe someone else can explain why this happens...but, all it took for me was to rename .css file(s) to .scss. 也许其他人可以解释为什么会发生这种情况......但是,我只需要将.css文件重命名为.scss。 Then, everything compiled correctly and all was right in the world. 然后,一切都正确编译,一切都在世界上。

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

setting these in config/envirnoments/production.rb fixed a similar problem for me with apache server 在config / envirnoments / production.rb中设置这些为apache服务器修复了类似的问题

I would not set config.assets.compile = true this has performance implications (but it does work). 我不会设置config.assets.compile = true这会影响性能(但确实有效)。

As outlined here: https://stackoverflow.com/a/16882028/647427 如下所述: https//stackoverflow.com/a/16882028/647427

When using the asset pipeline, paths to assets must be re-written and sass-rails provides -url and -path helpers (hyphenated in Sass, underscored in Ruby) for the following asset classes: image, font, video, audio, JavaScript and stylesheet.

image-url("rails.png") becomes url(/assets/rails.png)
image-path("rails.png") becomes "/assets/rails.png"
The more generic form can also be used but the asset path and class must both be specified:

asset-url("rails.png", image) becomes url(/assets/rails.png)
asset-path("rails.png", image) becomes "/assets/rails.png"

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

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