Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails rake assets:precompile for production

Tags:

I'm trying to precompile the assets for my app to deploy to Heroku but have to following error.

When running:

RAILS_ENV=production bundle exec rake assets:precompile 

Error:

/bin/rake assets:precompile:all RAILS_ENV=production RAILS_GROUPS=assets rake aborted! Please install the postgresql adapter: `gem install activerecord-postgresql-adapter` (pg is not part of the bundle. Add it to Gemfile.) 

Because I use in development SQLite and in production Postgresql the following Gemfile

gem "rails", "~> 3.1.0"  group :production do   gem 'pg' end  group :development, :test do   gem 'sqlite3' end  gem 'sass-rails', "~> 3.1.0"  group :assets do   gem 'coffee-rails', "~> 3.1.0"   gem 'uglifier'   gem 'compass', '~> 0.12.alpha.0'   gem 'html5-boilerplate' end 

I tried a lot but can't get this working.

I don't know if this is important but my database.yml looks like:

production:   adapter: postgresql   host: localhost   database: db   encoding: unicode   username: user   password: '' 
like image 975
Kieran Klaassen Avatar asked Oct 16 '11 21:10

Kieran Klaassen


People also ask

How do you Precompile assets Rails in production?

To compile your assets locally, run the assets:precompile task locally on your app. Make sure to use the production environment so that the production version of your assets are generated. A public/assets directory will be created. Inside this directory you'll find a manifest.

What does rake assets Precompile do?

rake assets:precompile. We use rake assets:precompile to precompile our assets before pushing code to production. This command precompiles assets and places them under the public/assets directory in our Rails application.

How does Rails asset pipeline work?

The asset pipeline provides a framework to concatenate and minify or compress JavaScript and CSS assets. It also adds the ability to write these assets in other languages and pre-processors such as CoffeeScript, Sass, and ERB.


1 Answers

Old question but the accepted answer doesn't really answer the question - and I just found this in a search so I guess it's relevant.

The reason for the error is that gem 'pg' is in the production gem group.
When you run rake assets:precompile the production environment is accessed. So it is trying to load the production environment but you don't have all of the dependencies installed.

Running RAILS_ENV=production bundle exec rails server would probably give you a similar error.

I can think of two different solutions

1) Look to see if you have a .bundle/config file in your app's root. If you do, check if it says WITHOUT :production or similar. Either remove that line or the whole .bundle directory and run bundle again.

2) in Gemfile

gem :development, :production do   gem 'pg' end 

while removing the :production group
run bundle again

Sorry to bring up old stuff...

like image 69
mraaroncruz Avatar answered Sep 19 '22 15:09

mraaroncruz