Heroku and Precompiled Assets

419 views Asked by At

I have a Rails 4 app. One of my javascript assets uses ruby to get all Users.

A problem arises when I try to upload to heroku. It says that it cannot precompile that asset because the database does not yet exist. What can I do short of removing that asset, uploading to heroku to migrate the database, and then putting it back?

Thanks!

1

There are 1 answers

0
Elvn On BEST ANSWER

Compile assets locally

To solve this chicken and egg problem with the database in deploy, do not invoke precompile in production, precompile on development machine for production prior to push. With a functional code base pushed, you can run "$heroku db:xxx" commands on Heroku.

  1. Turn off precompile assets in production.rb:
config.assets.compile = false
  1. Precompile assets for production on development machine:
$bundle exec rake assets:precompile RAILS_ENV=production
  1. Now your code glob will include your precompiled assets, so push it.
$heroku push

On deploy to Heroku, you're looking for this line:

    -----> Preparing Rails asset pipeline
           Detected manifest.yml, assuming assets were compiled locally
  1. At this point you can create your production database on Heroku
$heroku db:migrate

I think these steps will solve your problem. Please let me know.

Caveat

Once you disable precompile, you will have to remember to always precompile for production on your dev system prior to deploy to production.

Javascript compilation into asset pipeline

Since you're specifically speaking of js precompile, And I haven't tried this, but it seems these types of precompile js-specific lines should also be commented out in production.rb if you have them. There are the same type of js directives in assets. rb, again I don't know if these need to be cleared or not. (The main point is to do a local asset precompile, before the push to Heroku.)

  # Precompile additional assets.
  # application.js, application.css, and all non-JS/CSS in app/assets folder are already added.
  # config.assets.precompile += %w( xxxx.js )

Heroku docs

https://devcenter.heroku.com/articles/rails-asset-pipeline#compiling-assets-locally