Ask A Question


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.

  access_key_id: 123
  secret_access_key: 345

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

  => nil 

Traceback (most recent call last):
    1: from (irb):2
NoMethodError (undefined method `[]' for nil:NilClass)


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:

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:


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:


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:


In Rails, I had to use the following syntax to echo the values in the rails console: Rails.application.credentials.dig(Rails.env.to_sym, :stripe, :stripe_public_key)


I know it's been a while, but thanks Corinn! I needed to copy my credentials into a temp file, delete the existing credential files, run EDITOR=vi rails credentials:edit --environment production, delete the temp contents, and paste my old data in, per one of your links.


I had the exact same issue as Nino. For me the reason was that I accidentally created global credentials instead of specifically for the development environment. So I had a master.key file generated. what worked for me was to delete the master.key, credentials.yml.enc file and basically any credential file that I generated before (I had many attempts, so I also had a development.yml.enc and development.key). I deleted all of those and then ran EDITOR="code --wait" bin/rails credentials:edit --environment=development and it finally worked. I added the keys to the file. When I tried to access Rails.application.credentials.twitter for the millionth time, it finally gave me back the api keys

Join the discussion
Create an account Log in

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

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

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