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

☆樱花仙子☆ 提交于 2019-11-28 15:32:14
user2481435

I just now (June 13 2013)got this answer from Heroku devs whose support guided me across the barriers. This is how I got my css display from localhost working in my Heroku app.

"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." (Thanks Bret and Neil great news)

In /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.

Do a git add . and git commit.

Make sure that /config/database.yml has:

production:
  adapter: postgresql
  encoding: unicode
  database: Your_appname_production

You will need this info for the env command below.

Make sure you have gem 'pg' in production in your Gemfile Do another git commit.

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.

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.

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

JasmineOT

Just run bundle exec rake assets:precompile before pushing to heroku

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.

Your styles should start loading.

First of all upgrade from Rails beta to the latest release.

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).

Set it back to the default true

ruby config.assets.initialize_on_precompile = true

Then enable user-env-compile for the app on heroku:

# 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.

snowblindzz

Set config.assets.compile=true in the file /config/environments/production.rb:

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. In my own experience, this happens if you push out an assets folder that contains both .css and .scss file types. Maybe someone else can explain why this happens...but, all it took for me was to rename .css file(s) to .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

cman77

I would not set config.assets.compile = true this has performance implications (but it does work).

As outlined here: 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"
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!