6

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.

David Lee
  • 571
  • 6
  • 20

2 Answers2

3

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)

nuaky
  • 2,036
  • 1
  • 14
  • 20
  • "Read the doc" they say. Well where in the docs is ```Rails.env.to_sym``` ? Thanks for this. – David Lee Aug 19 '20 at 19:33
  • Nope this actually does not work either. Can't even deploy, `RAILS_ENV=production bundle exec rake assets:precompile` won't work. Work around to precompile for production is using dig method. Sure, then can deploy but that ain't working either. It's a waist. 2 days now. Very frustrating how they closed this issue too. – Elias Glyptis Apr 01 '21 at 03:42
0

I had to do Rails.application.credentials.stripe[:stripe_public_key]