flobacca
flobacca

Reputation: 978

How do I access the encrypted credentials in development mode?

I have this code in the controller

def index
  puts Rails.application.credentials.admin_email
  if (current_admin && current_admin.email == Rails.application.credentials.admin_email)
    #do this
  else
    #do that
  end
end 

The else branch always happens. I believe this is because I am not getting the string from the encrypted file back, as the puts line never prints anything.

When I type Rails.application.credentials.admin_email in the rails console, I get the correct response, "[email protected]"

My config/credentials.yml.enc file looks like the following:

admin_email: [email protected]

I have also tried changing the config/credentials.yml.enc file to be:

development:
  admin_email: [email protected]

And have changed the code to be

puts Rails.application.credentials[:admin_email]
if (current_admin && current_admin.email == Rails.application.credentials[:admin_email]

Then in the rails console I get the correct [email protected] using

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

, but still nothing shows up from the development code when running rails s.

Upvotes: 3

Views: 2240

Answers (2)

jpgeek
jpgeek

Reputation: 5291

Looks like you might have a typo/ copy paste error.

If you still have the credentials file in this format:

development:
  admin_email: [email protected]

Then you should be accessing it with

Rails.application.credentials.development[:admin_email]

In your question though under "And have changed the code to be" you still have the old version:

puts Rails.application.credentials[:admin_email]
if (current_admin && current_admin.email == Rails.application.credentials[:admin_email]

This should be:

puts Rails.application.credentials.development[:admin_email]
if (current_admin && current_admin.email == Rails.application.credentials.development[:admin_email]

Upvotes: 4

LHH
LHH

Reputation: 3323

This is the correct way to get environment specific credentials and that's why you got the credentials by using below code:-

Rails.application.credentials.admin_email

for more please check here https://github.com/rails/rails/issues/31349

OR https://github.com/rails/rails/pull/30067

Upvotes: 0

Related Questions