Skip to main content

Rails credentials returning nil

General • Asked by Nino Rosella

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.


Login or Create An Account to join the conversation.

Subscribe to the newsletter

Join 22,346+ developers who get early access to new screencasts, articles, guides, updates, and more.

    By clicking this button, you agree to the GoRails Terms of Service and Privacy Policy.

    More of a social being? We're also on Twitter and YouTube.