Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to manage credentials for different environments in Rails 5.2?

I'm (still/always) new to Rails. While I was building my project Rails upgraded to v5.2 with encrypted credentials. Now I'm about to deploy to production and found that I do not understand how to separate production credentials from development. And I'm not sure that I know how I would do it in v5.1. So how to make it in Rails 5.2?

like image 552
Denis Avatar asked Nov 19 '25 10:11

Denis


2 Answers

I solved my issue using this code

If in your credentials file like this

development: 
    stripe_secret_key: 123
production:
    stripe_secret_key: 345
test:
    stripe_secret_key: 678

1'st solution:

Rails.application.credentials[Rails.env.to_sym][:stripe_secret_key]

2'nd solution:

open application.rb file and define self.credentials method

module ModuleName
    class Application < Rails::Application
        def self.credentials
            @credentials ||= Rails.application.credentials[Rails.env.to_sym]
        end
    end 
end

And use

ModuleName.credentials[:stripe_secret_key]

More info please refer link

like image 161
Mamta Patel Avatar answered Nov 22 '25 01:11

Mamta Patel


I've found the rails-env-credentials gem useful for this. Not sure how compatible it will be with the upcoming Rails 6 support but its been working great for my app.

like image 44
Eric Anderson Avatar answered Nov 22 '25 01:11

Eric Anderson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!