Rails 3 /设置自定义环境变量

时间:2022-05-30 22:42:58

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 when the environment is the production environment. I want to specify both values in my code (hardwired), and have rails know which value to assign to the variable based on which environment is running. How do I do this?

我正在尝试创建一个rails应用程序,它在环境是开发环境时为变量分配一个值,在环境是生产环境时为该变量分配另一个值。我想在我的代码中指定两个值(硬连线),并让rails知道根据正在运行的环境分配给变量的值。我该怎么做呢?

In case it is important, I later access that variable and return its value in a class method of a model.

如果它很重要,我稍后访问该变量并在模型的类方法中返回其值。

4 个解决方案

#1


25  

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

然后在您的应用程序中,调用变量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.

当您在开发模式下运行时,这将访问http://test.domain.com网址。

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

当您在生产模式下运行时,这将访问http://www.domain.com网址。

#2


15  

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:

我喜欢在YAML中存储设置。要根据环境进行不同的设置,使用默认值,您可以使用这样的初始化文件(例如config / initializers / application_config.rb):

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

…and then in config/application_config.yml :

...然后在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]

...然后,用APP_CONFIG [:my_setting]拉出设置

#3


3  

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

我在Rails 3.2中使用Yettings gem,它允许我将我的应用程序变量存储在config / yettings.yml中,如下所示:

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"

#4


-2  

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:

将值存储在config / application.rb中的全局变量中,例如:

$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.

您还可以在config / environments / files中分配变量,而不是在application.rb中基于ond Rails.env进行决策。取决于你的情况。

#1


25  

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

然后在您的应用程序中,调用变量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.

当您在开发模式下运行时,这将访问http://test.domain.com网址。

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

当您在生产模式下运行时,这将访问http://www.domain.com网址。

#2


15  

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:

我喜欢在YAML中存储设置。要根据环境进行不同的设置,使用默认值,您可以使用这样的初始化文件(例如config / initializers / application_config.rb):

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

…and then in config/application_config.yml :

...然后在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]

...然后,用APP_CONFIG [:my_setting]拉出设置

#3


3  

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

我在Rails 3.2中使用Yettings gem,它允许我将我的应用程序变量存储在config / yettings.yml中,如下所示:

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"

#4


-2  

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:

将值存储在config / application.rb中的全局变量中,例如:

$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.

您还可以在config / environments / files中分配变量,而不是在application.rb中基于ond Rails.env进行决策。取决于你的情况。