Notifications
You’re not receiving notifications from this thread.
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
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.
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
In Rails 6.0.3.4, 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)