All threads / Rails credentials returning nil

Ask A Question

Notifications

You’re not receiving notifications from this thread.

Rails credentials returning nil

Nino Rosella asked in General

I have a Rails 6.0.0beta2 app and I'm having trouble reading my credentials.

When I do rails credentials:edit I can add credentials and they're saving just fine.

aws:
  access_key_id: 123
  secret_access_key: 345

However, when I run a console and try reading the credentials I get the following:

Rails.application.credentials[:aws]
  => nil 

or

Rails.application.credentials.aws[:access_key_id]
Traceback (most recent call last):
    1: from (irb):2
NoMethodError (undefined method `[]' for nil:NilClass)

or

Rails.application.credentials[:aws][:access_key_id]
Traceback (most recent call last):
    1: from (irb):3
NoMethodError (undefined method `[]' for nil:NilClass)

No matter what I try my credentials always seem to be nil.

If I run Rails.application.credentials.secret_key_base I get the correct result, so the file can be read just fine.

Hey Nino,

Maybe this is related to Rails scoping credentials to the environment again?

Hi Chris. Do you have any further info on that? Just tried looking through the release notes but can't find anything.

Ok so I found the following: https://github.com/rails/rails/pull/33521

I've now run rails credentials:edit --environment development and added the credentials in that file, but I'm still having the same issue when running a number of commands:

Rails.application.credentials[:development][:secret_key_base]
Rails.application.credentials.development.secret_key_base

I'm likely doing something wrong though...

I've not looked into this myself yet.

If you have scoped credentials, would you just need to access them directly without the environment since it knows which environment by the filename? Have you tried this:

Rails.application.credentials.secret_key_base

Which would load the secret_key_base from the development credentials file.

I know this comment is quite a bit late, but I just ran into this same issue but on the Stripe course when adding my stripe keys to the credentials doc.

I kept getting the same error 'NoMethodError (undefined method []' for nil:NilClass)' even though in the rails console Rails.application.credentials.stripe[:public_key] seemed to be working swimmingly.

What worked for me was actually regenerating my master key & credentials file. Once I popped the credentials in there and restarted the server, it seemed to work.

¯_(ツ)_/¯

Possibly helpful links:
https://blog.bigbinary.com/2019/07/03/rails-6-adds-support-for-multi-environment-credentials.html
https://gist.github.com/db0sch/19c321cbc727917bc0e12849a7565af9

Join the discussion

Want to stay up-to-date with Ruby on Rails?

Join 38,558+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    logo Created with Sketch.

    Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

    © 2020 GoRails, LLC. All rights reserved.