johncorser
johncorser

Reputation: 9842

How can I set secret global variables in Rails and put the file in .gitignore?

I have a rails app that connects to various APIs, and instead of putting my API keys directly in the Rails project, I'd like to make a separate file, secrets.rb, that contains global string variables for all of my API secret keys.

Where should I save this, and how can I make sure that these globals will be included where I need them in my project?

Upvotes: 0

Views: 357

Answers (2)

Alex Tatarnikov
Alex Tatarnikov

Reputation: 353

I think better way is a create config.yml and add to gitignore.

# config/config.yml

development:
 api_key = 'dev_key'

test:
 api_key = 'test_key'

# config/initializers/app_config.rb

AppConfig = YAML.load_file(Rails.root.join('config', 'config.yml'))[Rails.env]

Upvotes: 0

vich
vich

Reputation: 11896

You could create an initializer with your API constants and add it to .gitignore:

# config/initializers/secrets.rb

API_KEY = 'my_api_key'
API_SECRET_KEY = 'my_api_secret_key'

You could add them to config/environments/development.rb as suggested by @engineersmnky in the comments, but you typically want to add development.rb into version control.

Upvotes: 1

Related Questions