Rails credentials returning nil
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
Rails.application.credentials.aws[:access_key_id] Traceback (most recent call last): 1: from (irb):2 NoMethodError (undefined method `' for nil:NilClass)
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.
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:
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.
In Rails 18.104.22.168, 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.