Dave
Dave

Reputation: 19090

How do I set environment variables in my Rails application?

I’m using Rails 4.2.3 and trying to create environment variables accessible only in my application (as opposed to storing them in a ~/.bashrc file or some OS-specific solution). So I created the file “config/environment_variables.yml” with

development:
  GOOGLE_CLIENT_ID: 999995268318-o5ejue1pgtsjoid0f0k8r7lcksfse6hk.apps.googleusercontent.com
  GOOGLE_SECRET: 28bfoTU_RbRKkdxv7_wkNzw5
  FACEBOOK_KEY: 1588888667329742
  FACEBOOK_SECRET: 4444g1faeb11111e5392892d397b79f8e
production:
  GOOGLE_CLIENT_ID: 999995268318-o5ejue1pgtsjoid0f0k8r7lcksfse6hk.apps.googleusercontent.com
  GOOGLE_SECRET: 28bfoTU_RbRKkdxv7_wkNzw5
  FACEBOOK_KEY: 1588888667329742
  FACEBOOK_SECRET: 4444g1faeb11111e5392892d397b79f8e

and then I created the file “config/initializers/environment_variables.rb”

module EnvironmentVariablesExample
  class Application < Rails::Application
    config.before_configuration do
      env_file = Rails.root.join("config", 'environment_variables.yml').to_s

      if File.exists?(env_file)
        YAML.load_file(env_file)[Rails.env].each do |key, value|
          ENV[key.to_s] = value
        end # end YAML.load_file
      end # end if File.exists?
    end # end config.before_configuration
  end # end class
end # end module

but when I startup my server on my local machine with “rails s,” I get this error

Exiting
/Users/davea/Documents/workspace/pushupmoose/config/initializers/environment_variables.rb:8:in `[]=': no implicit conversion of Fixnum into String (TypeError)
    from /Users/davea/Documents/workspace/pushupmoose/config/initializers/environment_variables.rb:8:in `block (2 levels) in <class:Application>'
    from /Users/davea/Documents/workspace/pushupmoose/config/initializers/environment_variables.rb:7:in `each'
    from /Users/davea/Documents/workspace/pushupmoose/config/initializers/environment_variables.rb:7:in `block in <class:Application>'
    from /Users/davea/.rvm/gems/ruby-2.3.0/gems/activesupport-4.2.5/lib/active_support/lazy_load_hooks.rb:36:in `execute_hook'
    from /Users/davea/.rvm/gems/ruby-2.3.0/gems/activesupport-4.2.5/lib/active_support/lazy_load_hooks.rb:28:in `block in on_load'
    from /Users/davea/.rvm/gems/ruby-2.3.0/gems/activesupport-4.2.5/lib/active_support/lazy_load_hooks.rb:27:in `each'
    from /Users/davea/.rvm/gems/ruby-2.3.0/gems/activesupport-4.2.5/lib/active_support/lazy_load_hooks.rb:27:in `on_load'
    from /Users/davea/.rvm/gems/ruby-2.3.0/gems/railties-4.2.5/lib/rails/railtie/configuration.rb:53:in `before_configuration'
    from /Users/davea/Documents/workspace/pushupmoose/config/initializers/environment_variables.rb:3:in `<class:Application>'
    from /Users/davea/Documents/workspace/pushupmoose/config/initializers/environment_variables.rb:2:in `<module:EnvironmentVariablesExample>'
    from /Users/davea/Documents/workspace/pushupmoose/config/initializers/environment_variables.rb:1:in `<top (required)>'
    from /Users/davea/.rvm/gems/ruby-2.3.0/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:268:in `load'
    from /Users/davea/.rvm/gems/ruby-2.3.0/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:268:in `block in load'
    from /Users/davea/.rvm/gems/ruby-2.3.0/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:240:in `load_dependency'
    from /Users/davea/.rvm/gems/ruby-2.3.0/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:268:in `load'
    from /Users/davea/.rvm/gems/ruby-2.3.0/gems/railties-4.2.5/lib/rails/engine.rb:652:in `block in load_config_initializer'
    from /Users/davea/.rvm/gems/ruby-2.3.0/gems/activesupport-4.2.5/lib/active_support/notifications.rb:166:in `instrument'
    from /Users/davea/.rvm/gems/ruby-2.3.0/gems/railties-4.2.5/lib/rails/engine.rb:651:in `load_config_initializer'
    from /Users/davea/.rvm/gems/ruby-2.3.0/gems/railties-4.2.5/lib/rails/engine.rb:616:in `block (2 levels) in <class:Engine>'
    from /Users/davea/.rvm/gems/ruby-2.3.0/gems/railties-4.2.5/lib/rails/engine.rb:615:in `each'
    from /Users/davea/.rvm/gems/ruby-2.3.0/gems/railties-4.2.5/lib/rails/engine.rb:615:in `block in <class:Engine>'
    from /Users/davea/.rvm/gems/ruby-2.3.0/gems/railties-4.2.5/lib/rails/initializable.rb:30:in `instance_exec'
    from /Users/davea/.rvm/gems/ruby-2.3.0/gems/railties-4.2.5/lib/rails/initializable.rb:30:in `run'
    …

What am I doing wrong in my environment set up above? Thanks, - Dave

Upvotes: 5

Views: 13431

Answers (2)

SulmanWeb
SulmanWeb

Reputation: 611

You can use gem called dotenv-rails. The documentation is here: https://github.com/bkeepers/dotenv simply in Gemfile write:

gem 'dotenv-rails'

Then run bundle install. After that create a file in main directory of app named '.env.local' and in that file write:

export GOOGLE_CLIENT_ID=YOUR_ID

(ID without any quotes) And the point where you want to use just write:

ENV['GOOGLE_CLIENT_ID']

And for production just create another file '.env.production'. Remember to call '.env*' in gitignore.

For more information on environment variable in development and production checkout this blog post: https://sulmanbaig.com/blogs/using-environment-variables-in-rails-heroku-capistrano

Upvotes: 4

Shadwell
Shadwell

Reputation: 34774

I think the error is not because of how you're setting the value but because of the value you are setting.

Your FACEBOOK_KEY values are numeric which the YAML.load_file will respect. It will then try and store those numeric values in the ENV. However, ENV only likes string values.

2.2.1 :019 > ENV['test'] = 1
TypeError: no implicit conversion of Fixnum into String
from (irb):19:in `[]='
from (irb):19

2.2.1 :020 > ENV['test'] = '1'
=> "1"

So, if you wrap your FACEBOOK_KEY values in double quotes you should be okay:

development:
  FACEBOOK_KEY: "1588888667329742"

Upvotes: 1

Related Questions