Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It really comes down to wanting to avoid recording these values in your version control system. </p> <p>The approach I usually take is as follows:</p> <p>Add the following to your <code>config/application.rb</code>, inside the application class definition</p> <pre><code># Loads app config from /config/env_vars.yml require 'yaml' rails_root = Rails.root || File.dirname(__FILE__) + '/../..' config = YAML.load_file(rails_root.to_s + '/config/env_vars.yml') if config.key?(Rails.env) &amp;&amp; config[Rails.env].is_a?(Hash) config[Rails.env].each do |key, value| ENV[key] = value.to_s end end </code></pre> <p>Create a file in <code>config/</code> called <code>env_vars.yml</code>, and include the following</p> <pre><code>development: FB_APP_ID: APP_ID_HERE FB_APP_SECRET: APP_SECRET_HERE </code></pre> <p>replacing <code>APP_ID_HERE</code> and <code>APP_SECRET_HERE</code> with your api details. Add as many other values to this file as required (i.e. Twitter keys etc), and repeat for each environment (e.g. if you have different FB apps for dev, staging and production). These can then be referenced by in your controller as <code>ENV['FB_APP_ID']</code> etc.</p> <p>If using this approach, you can leave the env_vars.yml out of your VCS, thus meaning that you have not actually committed these values to your repository, while still making them available to your app.</p> <hr> <p>Finally, if you use Heroku, assigning these values as environmental variables is easy to do for each app (i.e. <code>heroku config:set FB_APP_ID=234234324234</code> - see <a href="https://devcenter.heroku.com/articles/config-vars" rel="nofollow">https://devcenter.heroku.com/articles/config-vars</a> for more details), saving the need to commit these values to the repo before pushing up to Heroku.</p> <h3>N.B. If using Heroku, there are a couple of modifications to the above approach</h3> <p>If excluding <code>env_vars.yml</code> from git, this file will not be available when pushed to Heroku, causing your app to crash on boot. The simplest solution is to modify the code added to <code>config/application.rb</code> as follows:</p> <pre><code># Loads app config from /config/env_vars.yml unless Rails.env == 'production' require 'yaml' rails_root = Rails.root || File.dirname(__FILE__) + '/../..' config = YAML.load_file(rails_root.to_s + "/config/env_vars.yml") if config.key?(Rails.env) &amp;&amp; config[Rails.env].is_a?(Hash) config[Rails.env].each do |key, value| ENV[key] = value.to_s end end end </code></pre> <p>By wrapping it in the <code>unless Rails.env == 'production'</code> conditional, it will be skipped if running on <code>production</code> (the default environment for Heroku). If using multiple environments for Heroku (such as <code>staging</code>, <code>uat</code>, <code>production</code>) this could be modified to</p> <pre><code>unless %w(staging uat production).include?(Rails.env) </code></pre> <p>Once this has been committed and pushed to Heroku, it will no longer be looking for the <code>env_vars.yml</code> file, leaving you free to set your config variables an an app-by-app basis via the <code>heroku config:set</code> Heroku toolbelt command.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload