Rails 3 / Setting Custom Environment Variables

后端 未结 4 1337
北荒
北荒 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: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.

提交回复
热议问题