Search code examples
ruby-on-railsdatabasepostgresqlruby-on-rails-5

rails db:setup rails aborted! undefined method `[]' for nil:NilClass I'm using Postgresql and rails 5


I'm trying to run rails db:setup, but something is wrong with Postresql or configuration. i'm using Rails 5.2.2, Linux machine, ruby 2.5.1p57.

Here is error i'm getting:

rails db:setup
rails aborted!
NoMethodError: undefined method `[]' for nil:NilClass
/home/cabox/workspace/rails-5-boilerplate/config/environments/development.rb:69:in `block in <main>'
/home/cabox/.rvm/gems/ruby-2.5.1/gems/railties-5.2.2/lib/rails/railtie.rb:216:in `instance_eval'
/home/cabox/.rvm/gems/ruby-2.5.1/gems/railties-5.2.2/lib/rails/railtie.rb:216:in `configure'
/home/cabox/workspace/rails-5-boilerplate/config/environments/development.rb:1:in `<main>'
/home/cabox/.rvm/gems/ruby-2.5.1/gems/bootsnap-1.3.2/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:21:in `require'
/home/cabox/.rvm/gems/ruby-2.5.1/gems/bootsnap-1.3.2/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:21:in `block in require_with_bootsnap_lfi'
/home/cabox/.rvm/gems/ruby-2.5.1/gems/bootsnap-1.3.2/lib/bootsnap/load_path_cache/loaded_features_index.rb:65:in `register'
/home/cabox/.rvm/gems/ruby-2.5.1/gems/bootsnap-1.3.2/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:20:in `require_with_bootsnap_lfi'
/home/cabox/.rvm/gems/ruby-2.5.1/gems/bootsnap-1.3.2/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:29:in `require'
/home/cabox/.rvm/gems/ruby-2.5.1/gems/activesupport-5.2.2/lib/active_support/dependencies.rb:291:in `block in require'
/home/cabox/.rvm/gems/ruby-2.5.1/gems/activesupport-5.2.2/lib/active_support/dependencies.rb:257:in `load_dependency'
/home/cabox/.rvm/gems/ruby-2.5.1/gems/activesupport-5.2.2/lib/active_support/dependencies.rb:291:in `require'
/home/cabox/.rvm/gems/ruby-2.5.1/gems/railties-5.2.2/lib/rails/engine.rb:602:in `block (2 levels) in <class:Engine>'
/home/cabox/.rvm/gems/ruby-2.5.1/gems/railties-5.2.2/lib/rails/engine.rb:601:in `each'
/home/cabox/.rvm/gems/ruby-2.5.1/gems/railties-5.2.2/lib/rails/engine.rb:601:in `block in <class:Engine>'
/home/cabox/.rvm/gems/ruby-2.5.1/gems/railties-5.2.2/lib/rails/initializable.rb:32:in `instance_exec'
/home/cabox/.rvm/gems/ruby-2.5.1/gems/railties-5.2.2/lib/rails/initializable.rb:32:in `run'
/home/cabox/.rvm/gems/ruby-2.5.1/gems/railties-5.2.2/lib/rails/initializable.rb:61:in `block in run_initializers'
/home/cabox/.rvm/gems/ruby-2.5.1/gems/railties-5.2.2/lib/rails/initializable.rb:50:in `each'
/home/cabox/.rvm/gems/ruby-2.5.1/gems/railties-5.2.2/lib/rails/initializable.rb:50:in `tsort_each_child'
/home/cabox/.rvm/gems/ruby-2.5.1/gems/railties-5.2.2/lib/rails/initializable.rb:60:in `run_initializers'
/home/cabox/.rvm/gems/ruby-2.5.1/gems/railties-5.2.2/lib/rails/application.rb:361:in `initialize!'
/home/cabox/workspace/rails-5-boilerplate/config/environment.rb:5:in `<main>'
/home/cabox/.rvm/gems/ruby-2.5.1/gems/bootsnap-1.3.2/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:21:in `require'
/home/cabox/.rvm/gems/ruby-2.5.1/gems/bootsnap-1.3.2/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:21:in `block in require_with_bootsnap_lfi'
/home/cabox/.rvm/gems/ruby-2.5.1/gems/bootsnap-1.3.2/lib/bootsnap/load_path_cache/loaded_features_index.rb:65:in `register'
/home/cabox/.rvm/gems/ruby-2.5.1/gems/bootsnap-1.3.2/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:20:in `require_with_bootsnap_lfi'
/home/cabox/.rvm/gems/ruby-2.5.1/gems/bootsnap-1.3.2/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:29:in `require'
/home/cabox/.rvm/gems/ruby-2.5.1/gems/activesupport-5.2.2/lib/active_support/dependencies.rb:291:in `block in require'
/home/cabox/.rvm/gems/ruby-2.5.1/gems/activesupport-5.2.2/lib/active_support/dependencies.rb:257:in `load_dependency'
/home/cabox/.rvm/gems/ruby-2.5.1/gems/activesupport-5.2.2/lib/active_support/dependencies.rb:291:in `require'
/home/cabox/.rvm/gems/ruby-2.5.1/gems/railties-5.2.2/lib/rails/application.rb:337:in `require_environment!'
/home/cabox/.rvm/gems/ruby-2.5.1/gems/railties-5.2.2/lib/rails/application.rb:520:in `block in run_tasks_blocks'
/home/cabox/.rvm/gems/ruby-2.5.1/gems/railties-5.2.2/lib/rails/commands/rake/rake_command.rb:23:in `block in perform'
/home/cabox/.rvm/gems/ruby-2.5.1/gems/railties-5.2.2/lib/rails/commands/rake/rake_command.rb:20:in `perform'
/home/cabox/.rvm/gems/ruby-2.5.1/gems/railties-5.2.2/lib/rails/command.rb:48:in `invoke'
/home/cabox/.rvm/gems/ruby-2.5.1/gems/railties-5.2.2/lib/rails/commands.rb:18:in `<main>'
/home/cabox/.rvm/gems/ruby-2.5.1/gems/bootsnap-1.3.2/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:21:in `require'
/home/cabox/.rvm/gems/ruby-2.5.1/gems/bootsnap-1.3.2/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:21:in `block in require_with_bootsnap_lfi'
/home/cabox/.rvm/gems/ruby-2.5.1/gems/bootsnap-1.3.2/lib/bootsnap/load_path_cache/loaded_features_index.rb:65:in `register'
/home/cabox/.rvm/gems/ruby-2.5.1/gems/bootsnap-1.3.2/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:20:in `require_with_bootsnap_lfi'
/home/cabox/.rvm/gems/ruby-2.5.1/gems/bootsnap-1.3.2/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:29:in `require'
/home/cabox/.rvm/gems/ruby-2.5.1/gems/activesupport-5.2.2/lib/active_support/dependencies.rb:291:in `block in require'
/home/cabox/.rvm/gems/ruby-2.5.1/gems/activesupport-5.2.2/lib/active_support/dependencies.rb:257:in `load_dependency'
/home/cabox/.rvm/gems/ruby-2.5.1/gems/activesupport-5.2.2/lib/active_support/dependencies.rb:291:in `require'
/home/cabox/workspace/rails-5-boilerplate/bin/rails:9:in `<top (required)>'
/home/cabox/.rvm/gems/ruby-2.5.1/gems/spring-2.0.2/lib/spring/client/rails.rb:28:in `load'
/home/cabox/.rvm/gems/ruby-2.5.1/gems/spring-2.0.2/lib/spring/client/rails.rb:28:in `call'
/home/cabox/.rvm/gems/ruby-2.5.1/gems/spring-2.0.2/lib/spring/client/command.rb:7:in `call'
/home/cabox/.rvm/gems/ruby-2.5.1/gems/spring-2.0.2/lib/spring/client.rb:30:in `run'
/home/cabox/.rvm/gems/ruby-2.5.1/gems/spring-2.0.2/bin/spring:49:in `<top (required)>'
/home/cabox/.rvm/gems/ruby-2.5.1/gems/spring-2.0.2/lib/spring/binstub.rb:31:in `load'
/home/cabox/.rvm/gems/ruby-2.5.1/gems/spring-2.0.2/lib/spring/binstub.rb:31:in `<top (required)>'
/home/cabox/workspace/rails-5-boilerplate/bin/spring:15:in `require'
/home/cabox/workspace/rails-5-boilerplate/bin/spring:15:in `<top (required)>'
bin/rails:3:in `load'
bin/rails:3:in `<main>'
Tasks: TOP => db:setup => db:schema:load_if_ruby => db:create => db:load_config => environment

Here is database.yml

default: &default
  adapter: postgresql
  encoding: unicode
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  username: postgres
  password: mypassword

development:
  <<: *default
  database: rails-5-boilerplate_development

Here is /environments/development.rb file:

Rails.application.configure do
  # Settings specified here will take precedence over those in config/application.rb.

  # for gem `rack-livereload` https://github.com/onesupercoder/rack-livereload
  config.middleware.insert_after ActionDispatch::Static, Rack::LiveReload

  # In the development environment your application's code is reloaded on
  # every request. This slows down response time but is perfect for development
  # since you don't have to restart the web server when you make code changes.
  config.cache_classes = false

  # Do not eager load code on boot.
  config.eager_load = false

  # Show full error reports.
  config.consider_all_requests_local = true

  # Enable/disable caching. By default caching is disabled.
  # Run rails dev:cache to toggle caching.
  if Rails.root.join('tmp', 'caching-dev.txt').exist?
    config.action_controller.perform_caching = true

    config.cache_store = :memory_store
    config.public_file_server.headers = {
      'Cache-Control' => "public, max-age=#{2.days.to_i}"
    }
  else
    config.action_controller.perform_caching = false

    config.cache_store = :null_store
  end

  # Store uploaded files on the local file system (see config/storage.yml for options)
  config.active_storage.service = :local

  # Print deprecation notices to the Rails logger.
  config.active_support.deprecation = :log

  # Raise an error on page load if there are pending migrations.
  config.active_record.migration_error = :page_load

  # Highlight code that triggered database queries in logs.
  config.active_record.verbose_query_logs = true

  # Debug mode disables concatenation and preprocessing of assets.
  # This option may cause significant delays in view rendering with a large
  # number of complex assets.
  config.assets.debug = true

  # Suppress logger output for asset requests.
  config.assets.quiet = true

  # Raises error for missing translations
  # config.action_view.raise_on_missing_translations = true

  # Use an evented file watcher to asynchronously detect changes in source code,
  # routes, locales, etc. This feature depends on the listen gem.
  config.file_watcher = ActiveSupport::EventedFileUpdateChecker

  # Email config
  config.action_mailer.raise_delivery_errors = true
  config.action_mailer.perform_caching = false
  config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
  config.action_mailer.delivery_method = :smtp
  config.action_mailer.perform_deliveries = true

  # EMAIL SMTP
  config.action_mailer.smtp_settings = {
    user_name: Rails.application.credentials.email[:user_name],
    password: Rails.application.credentials.email[:password],
    address: Rails.application.credentials.email[:host],
    port: 587,
    enable_starttls_auto: true,
    format: :html,
    from: '[email protected]'
  }
end

Solution

  • Rails 5.2 has introduced the concept ofcredentials. Basically, it allows us to create an encrypted credentials file that can be edited using a master.key configuration file which is used to decrypt credentials.yml.enc. In your case, the credentials file does not contain an email heading, which is probably expected to be of the following structure based on your config:

    email:
      username: <String>
      password: <String>
      host: <String>
    

    You can edit the credentials file using EDITOR=vim rails credentials:edit. In case you have issues in editing the credentials file, there is another discussion for the same in this question. The following links may help in understanding credentials better:

    1. Encrypted credentials in Rails 5.2
    2. Credentials in Rails 5.2