Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I believe you've got a slightly incorrect idea behind what your expectations for the config/application.rb is providing you. The ActiveRecord::Base and ActiveController::Base eigenclasses use the Rails::Application::Configuration class that is configured in config/application.rb. The attributes aren't available in classes that descend from either of the Base classes, nor their eigenclasses. This is why you are running into errors in ApplicationController. </p> <p>There are generally two ways to make configuration initializations in a Rails app. The first way is to create a configuration module and then load values into it via initializer:</p> <p>First, create a Twiter Config module:</p> <pre><code>#lib/twitter_config.rb module TwitterConfig def self.config @@config ||= {} end def self.config=(hash) @@config = hash end end </code></pre> <p>Create a YAML config file:</p> <pre><code># config/twitter.yaml development: &amp;base key: "foo" secret: "bar" test: &lt;&lt;: *base key: "foo2" production: &lt;&lt;: *base secret: "barbar" </code></pre> <p>Alternatively, if you don't intend to add config/twitter.yaml to your SCM, you can just skip this and set the key and secret via environment variables. This would be the suggested solution for an application with a public SCM repository deploying on Heroku.</p> <p>Then load and set the value via an initializer:</p> <pre><code>#config/initializers/01_twitter.rb require 'twitter_config' TwitterConfig.config = YAML.load_file("config/config.yml")[Rails.env].symbolize_keys </code></pre> <p>It's generally a best practice to number your initializer files as Rails will load them in order according to their filename. If you are initializing a datastore and that is critical for other steps, then it needs the lowest number. Alternatively, if you are using environment variables, this would be the init file:</p> <pre><code>#config/initializers/01_twitter.rb require 'twitter_config' TwitterConfig.config[:key] = ENV['twitter_config_key'] TwitterConfig.config[:secret] = ENV['twitter_config_secret'] </code></pre> <p>Throughout the Rails application, you now have access to the config values with TwitterConfig.config[:key] &amp; TwitterConfig.config[:secret]. You can include the module as well, just watch out for conflicts. </p> <p>You can also just load the values as a global constant. It feels a bit ugly to me though:</p> <pre><code>#config/application.rb TWITTER_CONFIG = YAML.load_file("config/twitter.yaml")[Rails.env] </code></pre>
    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