Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 6 accessing credentials does not work?

I am trying to set up a Stripe payment form for a Rails 6 project, and I am not able to access my stripe secret keys. I am getting an error NoMethodError (undefined method `[]' for nil:NilClass) What am I doing wrong?

Here's what I did

  1. Edit my credentials.yml.enc - Type this in the console EDITOR="code --wait" bin/rails credentials:edit

  2. Added my secret keys to credentials.yml.enc

    some_variable: secret_stuff
    development: 
      stripe:
        stripe_public_key: fake_key
        strip_secret_key: fake_key
    
  3. Closed the file and double checked it saved my code by opening up the file to verify it was saved (it was)

  4. Open up rails console and typed in Rails.application.credentials.some_variable
    I expected secret_stuff but I got nil.
    Typed in Rails.application.credentials.development[:stripe][:stripe_public_key]. I expected fake_key but I got NoMethodError (undefined method `[]' for nil:NilClass)

WHAT AM I DOING WRONG?

I can't display a credit card form, and I just want to get this up and running so I can charge people and become a millionaire.

like image 722
David Lee Avatar asked Aug 13 '20 18:08

David Lee


1 Answers

You probably accessing credentials environment wrong, this should work:

Rails.application.credentials[Rails.env.to_sym][:stripe][:stripe_public_key]

credentials.development => credentials[Rails.env.to_sym] (so you are accessing current environment varbiales)

like image 98
nuaky Avatar answered Oct 06 '22 19:10

nuaky