Rails 3 / Setting Custom Environment Variables

后端 未结 4 1329
北荒
北荒 2020-12-13 10:54

I am trying to create a rails application that assigns one value to a variable when the environment is the development environment, and another value to that same variable w

相关标签:
4条回答
  • 2020-12-13 11:18

    I like to store settings in YAML. To have different settings based on the environment, with defaults, you can have an initializer file (say config/initializers/application_config.rb) like this:

    APP_CONFIG = YAML.load_file("#{Rails.root}/config/application_config.yml")[Rails.env]
    

    …and then in config/application_config.yml :

    defaults: &defaults
        my_setting: "foobar"
    
    development:
        # add stuff here to override defaults.
        <<: *defaults
    
    test:
        <<: *defaults
    
    production:
        # add stuff here to override defaults.
        <<: *defaults
    

    …then, pull out the settings with APP_CONFIG[:my_setting]

    0 讨论(0)
  • 2020-12-13 11:23

    I use the Yettings gem in Rails 3.2, which allows me to store my application variables in config/yettings.yml like so:

    defaults: &defaults
      api_key: asdf12345lkj
      some_number: 999
      an_erb_yetting: <%= "erb stuff works" %>
      some_array:
        - element1
        - element2
    
    development:
      <<: *defaults
      api_key: api key for dev
    
    test:
      <<: *defaults
    
    production:
      <<: *defaults
    

    And access them like this:

    #/your_rails_app/config/yetting.yml in production
    Yetting.some_number #=> 999
    Yetting.api_key #=> "asdf12345lkj"
    
    0 讨论(0)
  • 2020-12-13 11:29

    You can do this with initializers.

    # config/initializers/configuration.rb
    class Configuration
      class << self
        attr_accessor :json_url
      end
    end
    
    # config/environments/development.rb
    #  Put this inside the ______::Application.configure do block
    config.after_initialize do
      Configuration.json_url = 'http://test.domain.com'
    end
    
    # config/environments/production.rb
    #  Put this inside the ______::Application.configure do block
    config.after_initialize do
      Configuration.json_url = 'http://www.domain.com'
    end
    

    Then in your application, call the variable Configuration.json_url

    # app/controller/listings_controller.rb
    def grab_json
      json_path = "#{Configuration.json_url}/path/to/json"
    end
    

    When you're running in development mode, this will hit the http://test.domain.com URL.

    When you're running in production mode, this will hit the http://www.domain.com URL.

    0 讨论(0)
  • 2020-12-13 11:43

    You can find out the environment like this:

    ruby-1.9.2-p0 > Rails.env
      => "development"
    

    Store the value in a global variable in your config/application.rb for example:

    $foo = "something"
    

    You could also assign the variable in your config/environments/ files instead of deciding based ond Rails.env in application.rb. Depends on your situation.

    0 讨论(0)
提交回复
热议问题