简体   繁体   中英

Precompiling assets with Rails_Env set as production

I'm trying to run $ rake assets:precompile RAILS_ENV=production in order to: 1) minify my assets (following this answer: How do I minify CSS in Rails 4? ) and 2) use the production level variables in my JS (ie, my JS has ENV['variable'] and when I run a regular rake assets:precompile and then push to Heroku, the code runs with the development version of ENV['variable'] rather than the production one.

However, I'm running into a problem where I get an error:

rake aborted!
Gem::LoadError: Specified 'postgresql' for database adapter, but the gem is not loaded. Add gem 'pg'` to your Gemfile (and ensure its version is at the minimum required by ActiveRecord).

The error is in response to this line in the JS file that I'm compiling:

var items_to_be_added = [{
  id: '<%= ItemName.where(name:"Jacket").first.id %>',
  name: "Jacket",
  number: 1
}

But the weird thing is that I ran a heroku run gem list and pg 0.15.1 is installed already.

Any help greatly appreciated!

You're going to have to specify Heroku's postgres database in your database.yml.

First, on the command line: heroku config

It should spit out DATABASE_URL: postgres://<username>:<password>@<host>/<database>

Put those values in your database.yml file so that your local environment knows how to connect to Heroku's database.

Then, asset precompilation should be able to query the production database and compile the assets.

Obviously, don't check this into version control.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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