11

I've pushed a Rails app to Heroku and keep on running into the following problem:

I'll save changes to my main css.scss file (in assets/stylesheets) or to images in assets/images, push to git, push that to heroku, and reload the page, only to find out that these assets haven't been loaded at all.

This was also a slight problem on the local server, but entering:

rake assets:precompile

and reloading the local server usually worked, whereas doing

heroku run rake assets:precompile

and then re-pushing does nothing. I've fished around for info and haven't found anything particularly helpful.

Of note, in my config/application.rb (some of these the result of said fishing around):

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

if defined?(Bundler)
  # If you precompile assets before deploying to production, use this line
  Bundler.require(*Rails.groups(:assets => %w(development test)))
  # If you want your assets lazily compiled in production, use this line
  # Bundler.require(:default, :assets, Rails.env)
end

in config/environments/production.rb:

# Disable Rails's static asset server (Apache or nginx will already do this)
config.serve_static_assets = false

# Compress JavaScripts and CSS
config.assets.compress = true

# Fallback to assets pipeline if a precompiled asset is missed
config.assets.compile = true

# Generate digests for assets URLs
config.assets.digest = true

Of additional possible interest, when I push to heroku, it says, among other things, this:

Preparing app for Rails asset pipeline
       Detected manifest.yml, assuming assets were compiled locally
-----> Rails plugin injection
       Injecting rails_log_stdout
       Injecting rails3_serve_static_assets

and

Installing dependencies using Bundler version 1.3.0.pre.5
Running: bundle install --without development:test --path vendor/bundle --binstubs vendor/bundle/bin --deployment

I learned with Rails 3 and don't really know how the assets pipeline differs from what was available in previous version, so sorry if I'm being an idiot and putting overlapping and/or contradictory settings in my config files.

Would appreciate any help. This has been a headache.

Sasha
  • 6,224
  • 10
  • 55
  • 102

1 Answers1

13

It looks like it could be that you are add your locally compiled assets to git and pushing them and as a result Heroku is not compiling your assets on push. Check to make sure you are not adding the public/assets directory to git.

Karl Baum
  • 282
  • 3
  • 9
  • 1
    So you mean add /public/assets to the .gitignore file? Do that AND "git rm" the directory? Or delete particular files within it? Sorry. I don't know what relation that directory has to the asset pipeline, so I'm not really sure how to retroactively un-add its bad components. – Sasha Feb 05 '13 at 00:13
  • 3
    Remove that directory from git and then also add it to the git ignore. This directory is where where's compiles all of the assets to when running rake assets:precompile. – Karl Baum Feb 05 '13 at 01:22
  • make sure to commit the deleted files in public/assets/ to master! so you push an empty public/assets/ directory to heroku. You should see heroku automatically call rake assets:precompile – Bruno Mar 01 '14 at 20:38
  • Should we still run `RAILS_ENV=production bundle exec rake assets:precompile` before pushing to Heroku? – starscream_disco_party Jul 30 '14 at 02:51
  • Also, are his changes to `application.rb` and `production.rb` really necessary if `public/assets/` is in `.gitignore`? – starscream_disco_party Jul 31 '14 at 14:14
  • I was having this same problem and this fix nailed it. This is probably a common occurrence and should be in the documentation. Thanks! – soultrust Jul 05 '16 at 21:07