简体   繁体   中英

What is the proper way to avoid the rake assets:precompile error on Heroku

I'm running on Rails 3.2.2 and deploying on a Cedar stack. I still get the following error:

-----> Writing config/database.yml to read from DATABASE_URL
-----> Preparing app for Rails asset pipeline
       Running: rake assets:precompile
       rake aborted!
       could not connect to server: Connection refused
       Is the server running on host "127.0.0.1" and accepting
       TCP/IP connections on port 5432?

Even the guide suggests setting config.assets.initialize_on_precompile to false, but mentions:

be sure to test rake assets:precompile locally before deploying. It may expose bugs where your assets reference application objects or methods, since those are still in scope in development mode regardless of the value of this flag.

Some articles such as this one suggest using Heroku labs . But even this, comes with its own implication, since it is still on beta.

I've read on several other sources online, including:

Rails 3.1 assets:precompile Connecting to Database

rake assets:precompile not working on heroku

All come with different solutions. So what is the proper way of avoiding this error? Anyone experience any big problems with Heroku labs? Is there a better approach?

PS . Just to be clear. Is running rake assets:precompile RAILS_ENV=development or rake assets:precompile RAILS_ENV=production the proper way of running this locally?

The issue stems from the fact that your application is trying to connect to postgres when you're running that rake task. The only way to get rid of the error is to either stop this from happening, or to use the user_env_compile add-on that you mention.

(1) I always add this as you suggested

config.assets.initialize_on_precompile = false

(2) But also, if using ActiveAdmin and/or Devise, exclude their routes when precompiling assets by coding routes.rb as follows

  unless ARGV.join.include?('assets:precompile')
    ActiveAdmin.routes(self)

    devise_for :admin_users, ...etc....
    devise_for :users, ...etc...

    devise_scope :user do
    get "/login", ..etc
  end

as per here and elsewhere

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