'Ruby On Rails - NoMethodError: undefined method `[]' for nil:NilClass

I'm trying to run on a new computer some ruby code and I receive the following error (rails db:migrate --trace)

** Invoke db:migrate (first_time)
** Invoke db:load_config (first_time)
** Invoke environment (first_time)
** Execute environment
rails aborted!
NoMethodError: undefined method `[]' for nil:NilClass
/Users/jairochavez/Documents/Rails/rfdf_bolivia/config/initializers/stripe.rb:2:in `<main>'
/Users/jairochavez/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/bootsnap-1.4.4/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:54:in `load'
/Users/jairochavez/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/bootsnap-1.4.4/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:54:in `load'
/Users/jairochavez/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/activesupport-5.2.3/lib/active_support/dependencies.rb:285:in `block in load'
/Users/jairochavez/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/activesupport-5.2.3/lib/active_support/dependencies.rb:257:in `load_dependency'
/Users/jairochavez/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/activesupport-5.2.3/lib/active_support/dependencies.rb:285:in `load'
/Users/jairochavez/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/railties-5.2.3/lib/rails/engine.rb:657:in `block in load_config_initializer'
/Users/jairochavez/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/activesupport-5.2.3/lib/active_support/notifications.rb:170:in `instrument'
/Users/jairochavez/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/railties-5.2.3/lib/rails/engine.rb:656:in `load_config_initializer'
/Users/jairochavez/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/railties-5.2.3/lib/rails/engine.rb:614:in `block (2 levels) in <class:Engine>'
/Users/jairochavez/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/railties-5.2.3/lib/rails/engine.rb:613:in `each'
/Users/jairochavez/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/railties-5.2.3/lib/rails/engine.rb:613:in `block in <class:Engine>'
/Users/jairochavez/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/railties-5.2.3/lib/rails/initializable.rb:32:in `instance_exec'
/Users/jairochavez/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/railties-5.2.3/lib/rails/initializable.rb:32:in `run'
/Users/jairochavez/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/railties-5.2.3/lib/rails/initializable.rb:61:in `block in run_initializers'
/Users/jairochavez/.rbenv/versions/2.6.3/lib/ruby/2.6.0/tsort.rb:228:in `block in tsort_each'
/Users/jairochavez/.rbenv/versions/2.6.3/lib/ruby/2.6.0/tsort.rb:350:in `block (2 levels) in each_strongly_connected_component'
/Users/jairochavez/.rbenv/versions/2.6.3/lib/ruby/2.6.0/tsort.rb:422:in `block (2 levels) in each_strongly_connected_component_from'
/Users/jairochavez/.rbenv/versions/2.6.3/lib/ruby/2.6.0/tsort.rb:431:in `each_strongly_connected_component_from'
/Users/jairochavez/.rbenv/versions/2.6.3/lib/ruby/2.6.0/tsort.rb:421:in `block in each_strongly_connected_component_from'
/Users/jairochavez/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/railties-5.2.3/lib/rails/initializable.rb:50:in `each'
/Users/jairochavez/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/railties-5.2.3/lib/rails/initializable.rb:50:in `tsort_each_child'
/Users/jairochavez/.rbenv/versions/2.6.3/lib/ruby/2.6.0/tsort.rb:415:in `call'
/Users/jairochavez/.rbenv/versions/2.6.3/lib/ruby/2.6.0/tsort.rb:415:in `each_strongly_connected_component_from'
/Users/jairochavez/.rbenv/versions/2.6.3/lib/ruby/2.6.0/tsort.rb:349:in `block in each_strongly_connected_component'
/Users/jairochavez/.rbenv/versions/2.6.3/lib/ruby/2.6.0/tsort.rb:347:in `each'
/Users/jairochavez/.rbenv/versions/2.6.3/lib/ruby/2.6.0/tsort.rb:347:in `call'
/Users/jairochavez/.rbenv/versions/2.6.3/lib/ruby/2.6.0/tsort.rb:347:in `each_strongly_connected_component'
/Users/jairochavez/.rbenv/versions/2.6.3/lib/ruby/2.6.0/tsort.rb:226:in `tsort_each'
/Users/jairochavez/.rbenv/versions/2.6.3/lib/ruby/2.6.0/tsort.rb:205:in `tsort_each'
/Users/jairochavez/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/railties-5.2.3/lib/rails/initializable.rb:60:in `run_initializers'
/Users/jairochavez/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/railties-5.2.3/lib/rails/application.rb:361:in `initialize!'
/Users/jairochavez/Documents/Rails/rfdf_bolivia/config/environment.rb:5:in `<main>'
/Users/jairochavez/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/bootsnap-1.4.4/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `require'
/Users/jairochavez/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/bootsnap-1.4.4/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `block in require_with_bootsnap_lfi'
/Users/jairochavez/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/bootsnap-1.4.4/lib/bootsnap/load_path_cache/loaded_features_index.rb:92:in `register'
/Users/jairochavez/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/bootsnap-1.4.4/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:21:in `require_with_bootsnap_lfi'
/Users/jairochavez/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/bootsnap-1.4.4/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in `require'
/Users/jairochavez/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/activesupport-5.2.3/lib/active_support/dependencies.rb:291:in `block in require'
/Users/jairochavez/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/activesupport-5.2.3/lib/active_support/dependencies.rb:257:in `load_dependency'
/Users/jairochavez/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/activesupport-5.2.3/lib/active_support/dependencies.rb:291:in `require'
/Users/jairochavez/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/railties-5.2.3/lib/rails/application.rb:337:in `require_environment!'
/Users/jairochavez/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/railties-5.2.3/lib/rails/application.rb:520:in `block in run_tasks_blocks'
/Users/jairochavez/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/rake-12.3.2/lib/rake/task.rb:273:in `block in execute'
/Users/jairochavez/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/rake-12.3.2/lib/rake/task.rb:273:in `each'
/Users/jairochavez/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/rake-12.3.2/lib/rake/task.rb:273:in `execute'
/Users/jairochavez/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/rake-12.3.2/lib/rake/task.rb:214:in `block in invoke_with_call_chain'
/Users/jairochavez/.rbenv/versions/2.6.3/lib/ruby/2.6.0/monitor.rb:230:in `mon_synchronize'
/Users/jairochavez/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/rake-12.3.2/lib/rake/task.rb:194:in `invoke_with_call_chain'
/Users/jairochavez/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/rake-12.3.2/lib/rake/task.rb:238:in `block in invoke_prerequisites'
/Users/jairochavez/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/rake-12.3.2/lib/rake/task.rb:236:in `each'
/Users/jairochavez/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/rake-12.3.2/lib/rake/task.rb:236:in `invoke_prerequisites'
/Users/jairochavez/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/rake-12.3.2/lib/rake/task.rb:213:in `block in invoke_with_call_chain'
/Users/jairochavez/.rbenv/versions/2.6.3/lib/ruby/2.6.0/monitor.rb:230:in `mon_synchronize'
/Users/jairochavez/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/rake-12.3.2/lib/rake/task.rb:194:in `invoke_with_call_chain'
/Users/jairochavez/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/rake-12.3.2/lib/rake/task.rb:238:in `block in invoke_prerequisites'
/Users/jairochavez/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/rake-12.3.2/lib/rake/task.rb:236:in `each'
/Users/jairochavez/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/rake-12.3.2/lib/rake/task.rb:236:in `invoke_prerequisites'
/Users/jairochavez/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/rake-12.3.2/lib/rake/task.rb:213:in `block in invoke_with_call_chain'
/Users/jairochavez/.rbenv/versions/2.6.3/lib/ruby/2.6.0/monitor.rb:230:in `mon_synchronize'
/Users/jairochavez/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/rake-12.3.2/lib/rake/task.rb:194:in `invoke_with_call_chain'
/Users/jairochavez/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/rake-12.3.2/lib/rake/task.rb:183:in `invoke'
/Users/jairochavez/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/rake-12.3.2/lib/rake/application.rb:160:in `invoke_task'
/Users/jairochavez/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/rake-12.3.2/lib/rake/application.rb:116:in `block (2 levels) in top_level'
/Users/jairochavez/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/rake-12.3.2/lib/rake/application.rb:116:in `each'
/Users/jairochavez/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/rake-12.3.2/lib/rake/application.rb:116:in `block in top_level'
/Users/jairochavez/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/rake-12.3.2/lib/rake/application.rb:125:in `run_with_threads'
/Users/jairochavez/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/rake-12.3.2/lib/rake/application.rb:110:in `top_level'
/Users/jairochavez/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/railties-5.2.3/lib/rails/commands/rake/rake_command.rb:23:in `block in perform'
/Users/jairochavez/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/rake-12.3.2/lib/rake/application.rb:186:in `standard_exception_handling'
/Users/jairochavez/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/railties-5.2.3/lib/rails/commands/rake/rake_command.rb:20:in `perform'
/Users/jairochavez/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/railties-5.2.3/lib/rails/command.rb:48:in `invoke'
/Users/jairochavez/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/railties-5.2.3/lib/rails/commands.rb:18:in `<main>'
/Users/jairochavez/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/bootsnap-1.4.4/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `require'
/Users/jairochavez/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/bootsnap-1.4.4/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `block in require_with_bootsnap_lfi'
/Users/jairochavez/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/bootsnap-1.4.4/lib/bootsnap/load_path_cache/loaded_features_index.rb:92:in `register'
/Users/jairochavez/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/bootsnap-1.4.4/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:21:in `require_with_bootsnap_lfi'
/Users/jairochavez/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/bootsnap-1.4.4/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in `require'
/Users/jairochavez/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/activesupport-5.2.3/lib/active_support/dependencies.rb:291:in `block in require'
/Users/jairochavez/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/activesupport-5.2.3/lib/active_support/dependencies.rb:257:in `load_dependency'
/Users/jairochavez/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/activesupport-5.2.3/lib/active_support/dependencies.rb:291:in `require'
/Users/jairochavez/Documents/Rails/rfdf_bolivia/bin/rails:9:in `<top (required)>'
/Users/jairochavez/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/spring-2.0.2/lib/spring/client/rails.rb:28:in `load'
/Users/jairochavez/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/spring-2.0.2/lib/spring/client/rails.rb:28:in `call'
/Users/jairochavez/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/spring-2.0.2/lib/spring/client/command.rb:7:in `call'
/Users/jairochavez/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/spring-2.0.2/lib/spring/client.rb:30:in `run'
/Users/jairochavez/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/spring-2.0.2/bin/spring:49:in `<top (required)>'
/Users/jairochavez/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/spring-2.0.2/lib/spring/binstub.rb:31:in `load'
/Users/jairochavez/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/spring-2.0.2/lib/spring/binstub.rb:31:in `<top (required)>'
/Users/jairochavez/.rbenv/versions/2.6.3/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
/Users/jairochavez/.rbenv/versions/2.6.3/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
/Users/jairochavez/Documents/Rails/rfdf_bolivia/bin/spring:15:in `<top (required)>'
bin/rails:3:in `load'
bin/rails:3:in `<main>'
Tasks: TOP => db:migrate => db:load_config => environment

config/initializers/stripe.rb

Rails.configuration.stripe = { 
  publishable_key: Rails.application.credentials[Rails.env.to_sym][:stripe][:public_key], 
  secret_key:      Rails.application.credentials[Rails.env.to_sym][:stripe][:secret_key] 
} 
Stripe.api_key = Rails.configuration.stripe[:secret_key]


Solution 1:[1]

From the stacktrace you posted, this line in particular appears to be the issue:

Rails.application.credentials[Rails.env.to_sym][:stripe][:public_key]

Since you're chaining multiple hash accesses, one of those objects is nil. You could try inspecting each of them by adding something like this to the top of the initializer:

puts Rails.application.credentials.inspect
puts Rails.application.credentials[Rails.env.to_sym].inspect
puts Rails.application.credentials[Rails.env.to_sym][:stripe].inspect

That will tell you which part is nil that shouldn't be, and you can update your credentials file accordingly.

Solution 2:[2]

Based on your configuration you should have your credentials.yml looking something like this:

stripe:
  secret_key: pk_123456789012345
  public_key: sk_abcdefghijklnmk

In your stripe.rb you missed [:secret_key]. It should look like this. But this won't work either when you are precompiling in production.

Stripe.api_key = Rails.application.credentials[Rails.env.to_sym][:stripe][:secret_key] 

Use dig method instead.

So for your stripe.rb

Stripe.api_key = Rails.application.credentials.dig(:stripe, :secret_key)

In your terminal run

RAILS_ENV=production rails c

In the console run

Rails.application.credentials.dig(:stripe, :secret_key)

Now you should get a value.

This was an issue I ran into and suffered until I figure it out. It seems to be an issue with rails code base. Not sure we need to load credentials to compile assets. Anyways, they close this issue but I am not sure why they closed it. Here is the link to the issue. https://github.com/rails/rails/issues/32947

Solution 3:[3]

When cloning an existing repo, you might run into this error if you do not have a config/master.key file, (which is not checked in to source control, e.g. GitHub). This is because, as @Elias Glyptis pointed out, credentials are needed to compile assets, (as documented in Rails #32947).

Solution 4:[4]

config/initializer/stripe.rb

Stripe.api_key = Rails.application.credentials.dig(Rails.env.to_sym, :stripe, :secret)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 supremebeing7
Solution 2
Solution 3 mefryar
Solution 4 antoniolulee