无法在本地预编译资产

时间:2018-05-22 07:38:36

标签: ruby-on-rails ruby heroku

我使用Heroku运行我的Ruby on Rails应用程序,我想预编译资产。

在我的Gemfile

group :development, :test do
  gem 'sqlite3'
end

group :production do
  gem 'pg'
end

config/database.yml

default: &default
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  timeout: 5000

development:
  <<: *default
  adapter: sqlite3
  database: db/development.sqlite3

test:
  <<: *default
  adapter: sqlite3
  database: db/test.sqlite3

production:
  <<: *default
  url: <%= ENV['DATABASE_URL'] %>

config/application.rbconfig/environments/production.rb

# config/application.rb
config.serve_static_files = true
config.assets.initialize_on_precompile = false

# config/environments/production.rb
config.assets.js_compressor = :uglifier
config.assets.css_compressor = :scss
config.assets.compile = true
config.assets.digest = true

然后,我执行了以下命令并出现错误:

$ export DATABASE_URL=$(heroku config:get DATABASE_URL)
$ RAILS_ENV=production bundle exec rake assets:precompile
rake aborted!
ActiveRecord::AdapterNotSpecified: database configuration does not specify adapter
/Users/nakatam/develop/naughie/vendor/bundle/ruby/2.5.0/gems/activerecord-5.1.6/lib/active_record/connection_adapters/connection_specification.rb:182:in `spec'
/Users/nakatam/develop/naughie/vendor/bundle/ruby/2.5.0/gems/activerecord-5.1.6/lib/active_record/connection_adapters/abstract/connection_pool.rb:880:in `establish_connection'
/Users/nakatam/develop/naughie/vendor/bundle/ruby/2.5.0/gems/activerecord-5.1.6/lib/active_record/connection_handling.rb:58:in `establish_connection'
/Users/nakatam/develop/naughie/vendor/bundle/ruby/2.5.0/gems/activerecord-5.1.6/lib/active_record/railtie.rb:124:in `block (2 levels) in <class:Railtie>'
/Users/nakatam/develop/naughie/vendor/bundle/ruby/2.5.0/gems/activesupport-5.1.6/lib/active_support/lazy_load_hooks.rb:69:in `instance_eval'
/Users/nakatam/develop/naughie/vendor/bundle/ruby/2.5.0/gems/activesupport-5.1.6/lib/active_support/lazy_load_hooks.rb:69:in `block in execute_hook'
/Users/nakatam/develop/naughie/vendor/bundle/ruby/2.5.0/gems/activesupport-5.1.6/lib/active_support/lazy_load_hooks.rb:60:in `with_execution_control'
/Users/nakatam/develop/naughie/vendor/bundle/ruby/2.5.0/gems/activesupport-5.1.6/lib/active_support/lazy_load_hooks.rb:65:in `execute_hook'
/Users/nakatam/develop/naughie/vendor/bundle/ruby/2.5.0/gems/activesupport-5.1.6/lib/active_support/lazy_load_hooks.rb:50:in `block in run_load_hooks'
/Users/nakatam/develop/naughie/vendor/bundle/ruby/2.5.0/gems/activesupport-5.1.6/lib/active_support/lazy_load_hooks.rb:49:in `each'
/Users/nakatam/develop/naughie/vendor/bundle/ruby/2.5.0/gems/activesupport-5.1.6/lib/active_support/lazy_load_hooks.rb:49:in `run_load_hooks'
/Users/nakatam/develop/naughie/vendor/bundle/ruby/2.5.0/gems/activerecord-5.1.6/lib/active_record/base.rb:326:in `<module:ActiveRecord>'
/Users/nakatam/develop/naughie/vendor/bundle/ruby/2.5.0/gems/activerecord-5.1.6/lib/active_record/base.rb:25:in `<top (required)>'
/Users/nakatam/develop/naughie/vendor/bundle/ruby/2.5.0/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:292:in `require'
/Users/nakatam/develop/naughie/vendor/bundle/ruby/2.5.0/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:292:in `block in require'
/Users/nakatam/develop/naughie/vendor/bundle/ruby/2.5.0/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:258:in `load_dependency'
/Users/nakatam/develop/naughie/vendor/bundle/ruby/2.5.0/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:292:in `require'
/Users/nakatam/develop/naughie/app/models/application_record.rb:1:in `<top (required)>'
/Users/nakatam/develop/naughie/vendor/bundle/ruby/2.5.0/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:292:in `require'
/Users/nakatam/develop/naughie/vendor/bundle/ruby/2.5.0/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:292:in `block in require'
/Users/nakatam/develop/naughie/vendor/bundle/ruby/2.5.0/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:258:in `load_dependency'
/Users/nakatam/develop/naughie/vendor/bundle/ruby/2.5.0/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:292:in `require'
/Users/nakatam/develop/naughie/vendor/bundle/ruby/2.5.0/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:379:in `block in require_or_load'
/Users/nakatam/develop/naughie/vendor/bundle/ruby/2.5.0/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:36:in `block in load_interlock'
/Users/nakatam/develop/naughie/vendor/bundle/ruby/2.5.0/gems/activesupport-5.1.6/lib/active_support/dependencies/interlock.rb:12:in `block in loading'
/Users/nakatam/develop/naughie/vendor/bundle/ruby/2.5.0/gems/activesupport-5.1.6/lib/active_support/concurrency/share_lock.rb:149:in `exclusive'
/Users/nakatam/develop/naughie/vendor/bundle/ruby/2.5.0/gems/activesupport-5.1.6/lib/active_support/dependencies/interlock.rb:11:in `loading'
/Users/nakatam/develop/naughie/vendor/bundle/ruby/2.5.0/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:36:in `load_interlock'
/Users/nakatam/develop/naughie/vendor/bundle/ruby/2.5.0/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:357:in `require_or_load'
/Users/nakatam/develop/naughie/vendor/bundle/ruby/2.5.0/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:511:in `load_missing_constant'
/Users/nakatam/develop/naughie/vendor/bundle/ruby/2.5.0/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:202:in `const_missing'
/Users/nakatam/develop/naughie/app/models/user.rb:1:in `<top (required)>'
/Users/nakatam/develop/naughie/vendor/bundle/ruby/2.5.0/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:292:in `require'
/Users/nakatam/develop/naughie/vendor/bundle/ruby/2.5.0/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:292:in `block in require'
/Users/nakatam/develop/naughie/vendor/bundle/ruby/2.5.0/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:258:in `load_dependency'
/Users/nakatam/develop/naughie/vendor/bundle/ruby/2.5.0/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:292:in `require'
/Users/nakatam/develop/naughie/vendor/bundle/ruby/2.5.0/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:379:in `block in require_or_load'
/Users/nakatam/develop/naughie/vendor/bundle/ruby/2.5.0/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:36:in `block in load_interlock'
/Users/nakatam/develop/naughie/vendor/bundle/ruby/2.5.0/gems/activesupport-5.1.6/lib/active_support/dependencies/interlock.rb:12:in `block in loading'
/Users/nakatam/develop/naughie/vendor/bundle/ruby/2.5.0/gems/activesupport-5.1.6/lib/active_support/concurrency/share_lock.rb:149:in `exclusive'
/Users/nakatam/develop/naughie/vendor/bundle/ruby/2.5.0/gems/activesupport-5.1.6/lib/active_support/dependencies/interlock.rb:11:in `loading'
/Users/nakatam/develop/naughie/vendor/bundle/ruby/2.5.0/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:36:in `load_interlock'
/Users/nakatam/develop/naughie/vendor/bundle/ruby/2.5.0/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:357:in `require_or_load'
/Users/nakatam/develop/naughie/vendor/bundle/ruby/2.5.0/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:511:in `load_missing_constant'
/Users/nakatam/develop/naughie/vendor/bundle/ruby/2.5.0/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:202:in `const_missing'
/Users/nakatam/develop/naughie/vendor/bundle/ruby/2.5.0/gems/activesupport-5.1.6/lib/active_support/inflector/methods.rb:269:in `const_get'
/Users/nakatam/develop/naughie/vendor/bundle/ruby/2.5.0/gems/activesupport-5.1.6/lib/active_support/inflector/methods.rb:269:in `block in constantize'
/Users/nakatam/develop/naughie/vendor/bundle/ruby/2.5.0/gems/activesupport-5.1.6/lib/active_support/inflector/methods.rb:267:in `each'
/Users/nakatam/develop/naughie/vendor/bundle/ruby/2.5.0/gems/activesupport-5.1.6/lib/active_support/inflector/methods.rb:267:in `inject'
/Users/nakatam/develop/naughie/vendor/bundle/ruby/2.5.0/gems/activesupport-5.1.6/lib/active_support/inflector/methods.rb:267:in `constantize'
/Users/nakatam/develop/naughie/vendor/bundle/ruby/2.5.0/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:583:in `get'
/Users/nakatam/develop/naughie/vendor/bundle/ruby/2.5.0/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:614:in `constantize'
/Users/nakatam/develop/naughie/vendor/bundle/ruby/2.5.0/gems/devise-4.4.3/lib/devise.rb:316:in `get'
/Users/nakatam/develop/naughie/vendor/bundle/ruby/2.5.0/gems/devise-4.4.3/lib/devise/mapping.rb:83:in `to'
/Users/nakatam/develop/naughie/vendor/bundle/ruby/2.5.0/gems/devise-4.4.3/lib/devise/mapping.rb:78:in `modules'
/Users/nakatam/develop/naughie/vendor/bundle/ruby/2.5.0/gems/devise-4.4.3/lib/devise/mapping.rb:95:in `routes'
/Users/nakatam/develop/naughie/vendor/bundle/ruby/2.5.0/gems/devise-4.4.3/lib/devise/mapping.rb:162:in `default_used_route'
/Users/nakatam/develop/naughie/vendor/bundle/ruby/2.5.0/gems/devise-4.4.3/lib/devise/mapping.rb:72:in `initialize'
/Users/nakatam/develop/naughie/vendor/bundle/ruby/2.5.0/gems/devise-4.4.3/lib/devise.rb:346:in `new'
/Users/nakatam/develop/naughie/vendor/bundle/ruby/2.5.0/gems/devise-4.4.3/lib/devise.rb:346:in `add_mapping'
/Users/nakatam/develop/naughie/vendor/bundle/ruby/2.5.0/gems/devise-4.4.3/lib/devise/rails/routes.rb:243:in `block in devise_for'
/Users/nakatam/develop/naughie/vendor/bundle/ruby/2.5.0/gems/devise-4.4.3/lib/devise/rails/routes.rb:242:in `each'
/Users/nakatam/develop/naughie/vendor/bundle/ruby/2.5.0/gems/devise-4.4.3/lib/devise/rails/routes.rb:242:in `devise_for'
/Users/nakatam/develop/naughie/config/routes.rb:2:in `block in <top (required)>'
/Users/nakatam/develop/naughie/vendor/bundle/ruby/2.5.0/gems/actionpack-5.1.6/lib/action_dispatch/routing/route_set.rb:436:in `instance_exec'
/Users/nakatam/develop/naughie/vendor/bundle/ruby/2.5.0/gems/actionpack-5.1.6/lib/action_dispatch/routing/route_set.rb:436:in `eval_block'
/Users/nakatam/develop/naughie/vendor/bundle/ruby/2.5.0/gems/actionpack-5.1.6/lib/action_dispatch/routing/route_set.rb:418:in `draw'
/Users/nakatam/develop/naughie/config/routes.rb:1:in `<top (required)>'
/Users/nakatam/develop/naughie/vendor/bundle/ruby/2.5.0/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:286:in `load'
/Users/nakatam/develop/naughie/vendor/bundle/ruby/2.5.0/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:286:in `block in load'
/Users/nakatam/develop/naughie/vendor/bundle/ruby/2.5.0/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:258:in `load_dependency'
/Users/nakatam/develop/naughie/vendor/bundle/ruby/2.5.0/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:286:in `load'
/Users/nakatam/develop/naughie/vendor/bundle/ruby/2.5.0/gems/railties-5.1.6/lib/rails/application/routes_reloader.rb:55:in `block in load_paths'
/Users/nakatam/develop/naughie/vendor/bundle/ruby/2.5.0/gems/railties-5.1.6/lib/rails/application/routes_reloader.rb:55:in `each'
/Users/nakatam/develop/naughie/vendor/bundle/ruby/2.5.0/gems/railties-5.1.6/lib/rails/application/routes_reloader.rb:55:in `load_paths'
/Users/nakatam/develop/naughie/vendor/bundle/ruby/2.5.0/gems/railties-5.1.6/lib/rails/application/routes_reloader.rb:18:in `reload!'
/Users/nakatam/develop/naughie/vendor/bundle/ruby/2.5.0/gems/railties-5.1.6/lib/rails/application/routes_reloader.rb:41:in `block in updater'
/Users/nakatam/develop/naughie/vendor/bundle/ruby/2.5.0/gems/activesupport-5.1.6/lib/active_support/file_update_checker.rb:81:in `execute'
/Users/nakatam/develop/naughie/vendor/bundle/ruby/2.5.0/gems/railties-5.1.6/lib/rails/application/routes_reloader.rb:42:in `updater'
/Users/nakatam/develop/naughie/vendor/bundle/ruby/2.5.0/gems/railties-5.1.6/lib/rails/application/routes_reloader.rb:31:in `execute_if_updated'
/Users/nakatam/develop/naughie/vendor/bundle/ruby/2.5.0/gems/railties-5.1.6/lib/rails/application/finisher.rb:128:in `block in <module:Finisher>'
/Users/nakatam/develop/naughie/vendor/bundle/ruby/2.5.0/gems/railties-5.1.6/lib/rails/initializable.rb:30:in `instance_exec'
/Users/nakatam/develop/naughie/vendor/bundle/ruby/2.5.0/gems/railties-5.1.6/lib/rails/initializable.rb:30:in `run'
/Users/nakatam/develop/naughie/vendor/bundle/ruby/2.5.0/gems/railties-5.1.6/lib/rails/initializable.rb:59:in `block in run_initializers'
/Users/nakatam/develop/naughie/vendor/bundle/ruby/2.5.0/gems/railties-5.1.6/lib/rails/initializable.rb:58:in `run_initializers'
/Users/nakatam/develop/naughie/vendor/bundle/ruby/2.5.0/gems/railties-5.1.6/lib/rails/application.rb:353:in `initialize!'
/Users/nakatam/develop/naughie/config/environment.rb:5:in `<top (required)>'
/Users/nakatam/develop/naughie/vendor/bundle/ruby/2.5.0/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:292:in `require'
/Users/nakatam/develop/naughie/vendor/bundle/ruby/2.5.0/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:292:in `block in require'
/Users/nakatam/develop/naughie/vendor/bundle/ruby/2.5.0/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:258:in `load_dependency'
/Users/nakatam/develop/naughie/vendor/bundle/ruby/2.5.0/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:292:in `require'
/Users/nakatam/develop/naughie/vendor/bundle/ruby/2.5.0/gems/railties-5.1.6/lib/rails/application.rb:329:in `require_environment!'
/Users/nakatam/develop/naughie/vendor/bundle/ruby/2.5.0/gems/railties-5.1.6/lib/rails/application.rb:445:in `block in run_tasks_blocks'
/Users/nakatam/develop/naughie/vendor/bundle/ruby/2.5.0/gems/sprockets-rails-3.2.1/lib/sprockets/rails/task.rb:62:in `block (2 levels) in define'
/Users/nakatam/develop/naughie/vendor/bundle/ruby/2.5.0/gems/rake-12.3.1/exe/rake:27:in `<top (required)>'
/Users/nakatam/.rbenv/versions/2.5.0/bin/bundle:23:in `load'
/Users/nakatam/.rbenv/versions/2.5.0/bin/bundle:23:in `<main>'
Tasks: TOP => environment
(See full trace by running task with --trace)

接下来,我尝试在本地安装pg gem(macOS),但失败了。

为什么会出现这样的错误?我应该怎么做才能预编译资产?

1 个答案:

答案 0 :(得分:0)

根据共享的规范,您似乎错过了database.yml

中生产设置的适配器设置
 default: &default
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  # adapter: sqlite3 #preferred to use the adapter settings here
  timeout: 5000

 development:
   <<: *default
   adapter: sqlite3
   database: db/development.sqlite3

 test:
  <<: *default
  adapter: sqlite3
  database: db/test.sqlite3

 production:
  <<: *default
  adapter: postgresql
  url: <%= ENV['DATABASE_URL'] %>

否则,在多个环境中使用相同的数据库时,最好在默认配置中指定适配器设置。