Ruby-on-rails 如何在 Rails 中定义自定义配置变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1450285/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 21:47:11  来源:igfitidea点击:

How to define custom configuration variables in rails

ruby-on-railsconfiguration

提问by Shiv

I was wondering how to add custom configuration variables to a rails application and how to access them in the controller, for e.g I wanna be able to define an upload_directory in the configuration files say development.rb and be able to access it in one of my controllers.

我想知道如何将自定义配置变量添加到 rails 应用程序以及如何在控制器中访问它们,例如,我希望能够在配置文件中定义一个 upload_directory 说 development.rb 并且能够在我的一个中访问它控制器。

Secondly I was planning to have S3 support for uploads in my application, if i wanted to add a yaml file with the s3 access, secret key, how do I initialize it in my Rails App and how do I access the values that I have defined in that config file.

其次,我计划在我的应用程序中为上传提供 S3 支持,如果我想添加一个带有 s3 访问权限、密钥的 yaml 文件,我如何在我的 Rails 应用程序中初始化它以及如何访问我定义的值在那个配置文件中。

采纳答案by khelll

Update 1

更新 1

Very recommended: I'm going with Rails Configgem nowadays for the fine grained control it provides.

非常推荐:我现在正在使用Rails Configgem,因为它提供了细粒度的控制。

Update2

更新2

If you want a quick solution, then check Hyman Pratt's answerbelow.

如果您想要快速解决方案,请查看下面Hyman Pratt 的回答

Although my original answer below still works, this answer is now outdated. I recommend looking at updates 1 and 2.

虽然我在下面的原始答案仍然有效,但这个答案现在已经过时了。我建议查看更新 1 和 2。

Original Answer:

原答案:

For a quick solution, watching the "YAML Configuration File" screen castby Ryan Bates should be very helpful.

对于快速解决方案,观看Ryan Bates的“YAML 配置文件”屏幕应该会很有帮助。

In summary:

总之:

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

# application.rb
if APP_CONFIG['perform_authentication']
  # Do stuff
end

回答by Hyman Pratt

In Rails 3, Application specific custom configuration data can be placed in the application configuration object. The configuration can be assigned in the initialization files or the environment files -- say for a given application MyApp:

在 Rails 3 中,特定于应用程序的自定义配置数据可以放置在应用程序配置对象中。可以在初始化文件或环境文件中分配配置 - 例如对于给定的应用程序MyApp

MyApp::Application.config.custom_config_variable = :my_config_setting

or

或者

Rails.configuration.custom_config_variable = :my_config_setting

To read the setting, simply call the configuration variable without setting it:

要读取设置,只需调用配置变量而不设置它:

Rails.configuration.custom_config_variable
=> :my_config_setting

UPDATE Rails 4

更新 Rails 4

In Rails 4 there a new way for this => http://guides.rubyonrails.org/configuring.html#custom-configuration

在 Rails 4 中有一种新方法 => http://guides.rubyonrails.org/configuring.html#custom-configuration

enter image description here

在此处输入图片说明

回答by Rob Dawson

In Rails 3.0.5, the following approach worked for me:

在 Rails 3.0.5 中,以下方法对我有用:

In config/environments/development.rb, write

config/environments/development.rb,写

config.custom_config_key = :config_value

The value custom_config_keycan then be referenced from other files using

custom_config_key然后可以使用其他文件引用该值

Rails.application.config.custom_config_key

回答by Alain Beauvois

This works in rails 3.1:

这适用于 rails 3.1:

in config/environment.rb (or in config/environments/.. to target a specific environment) :

在 config/environment.rb (或在 config/environments/.. 中以针对特定环境):

YourApp::Application.config.yourKey = 'foo'

This will be accessible in controller or views like this:

这将可以在控制器或这样的视图中访问:

YourApp::Application.config.yourKey

(YourApp should be replaced by your application name.)

(YourApp 应替换为您的应用程序名称。)

Note: It's Ruby code, so if you have a lot of config keys, you can do this :

注意:这是 Ruby 代码,所以如果你有很多配置键,你可以这样做:

in config/environment.rb :

在 config/environment.rb 中:

YourApp::Application.configure do
  config.something = foo
  config.....
  config....
  .
  config....
end

回答by pymkin

In Rails 4

在 Rails 4

Assuming you put your custom variables into a yaml file:

假设您将自定义变量放入 yaml 文件中:

# config/acme.yml
development:
  :api_user: 'joe'
  :api_pass: 's4cret'
  :timeout: 20

Create an initializer to load them:

创建一个初始化程序来加载它们:

# config/initializers/acme.rb
acme_config = Rails.application.config_for :acme

Rails.application.configure do
  config.acme = ActiveSupport::OrderedOptions.new
  config.acme.api_user = acme_config[:api_user]
  config.acme.api_pass = acme_config[:api_pass]
  config.acme.timeout  = acme_config[:timeout]
end

Now anywhere in your app you can access these values like so:

现在,您可以在应用程序的任何位置访问这些值,如下所示:

Rails.configuration.acme.api_user

It is convenient that Rails.application.config_for :acmewill load your acme.ymland use the correct environment.

Rails.application.config_for :acme将加载您acme.yml并使用正确的环境很方便。

回答by lakesare

Since Rails 4.2, without additional gems,you can load config/hi.ymlsimply by using Rails.application.config_for :hi.

从 Rails 4.2 开始,无需额外的 gems,您只需使用.yml即可加载config/hi.ymlRails.application.config_for :hi

For example:

例如:

  1. touch config/passwords.yml

        #config/passwords.yml
        development:
          username: 'a'
          password: 'b'
        production:
          username: 'aa'
          password: 'bb'
    
  1. touch config/passwords.yml

        #config/passwords.yml
        development:
          username: 'a'
          password: 'b'
        production:
          username: 'aa'
          password: 'bb'
    
  1. touch config/initializers/constants.rb

    #config/initializers/constants.rb
    AUTHENTICATION = Rails.application.config_for :passwords
    
  1. touch config/initializers/constants.rb

    #config/initializers/constants.rb
    AUTHENTICATION = Rails.application.config_for :passwords
    
  1. and now you can use AUTHENTICATIONconstant everywhere in your application:

    #rails c production
    :001> AUTHENTICATION['username'] => 'aa'
    
  2. then add passwords.ymlto .gitignore: echo /config/passwords.yml >> .gitignore, create an example file for your comfort cp /config/passwords.yml /config/passwords.example.ymland then just edit your example file in your production console with actual production values.

  1. 现在您可以AUTHENTICATION在应用程序的任何地方使用常量:

    #rails c production
    :001> AUTHENTICATION['username'] => 'aa'
    
  2. 然后将passwords.yml添加到.gitignore: echo /config/passwords.yml >> .gitignore,为您的舒适创建一个示例文件cp /config/passwords.yml /config/passwords.example.yml,然后在您的生产控制台中使用实际生产值编辑您的示例文件。

回答by smathy

I just wanted to update this for the latest cool stuff in Rails 4.2, you can now do this inside any of your config/**/*.rbfiles:

我只是想更新 Rails 4.2 中最新的很酷的东西,你现在可以在你的任何config/**/*.rb文件中这样做:

config.x.whatever.you.want = 42

...and this will be available in your app as:

...这将在您的应用程序中可用:

Rails.configuration.x.whatever.you.want

See more here: http://guides.rubyonrails.org/configuring.html#custom-configuration

在此处查看更多信息:http: //guides.rubyonrails.org/configuring.html#custom-configuration

回答by Flov

Check out this neat gem doing exactly that: https://github.com/mislav/choices

看看这个整洁的 gem 就是这样做的:https: //github.com/mislav/choices

This way your sensitive data won't be exposed in open source projects

这样你的敏感数据就不会暴露在开源项目中

回答by MikeH

If you use Heroku or otherwise have need to keep your application settings as environment variables, the figarogem is very helpful.

如果您使用 Heroku 或需要将您的应用程序设置保留为环境变量,那么figarogem 非常有用。

回答by johnmcaliley

I created a simple plugin for YAML settings: Yettings

我为 YAML 设置创建了一个简单的插件: Yettings

It works in a similar fashion to the code in khelll's answer, but you only need to add this YAML configuration file:

它的工作方式与 khell 的答案中的代码类似,但您只需要添加此 YAML 配置文件:

app/config/yetting.yml

The plugin dynamically creates a class that allows you to access the YML settings as class methods in your app like so:

该插件动态创建一个类,允许您在应用程序中将 YML 设置作为类方法访问,如下所示:

Yetting.your_setting

Also, if you want to use multiple settings files with unique names, you can place them in a subdirectory inside app/config like this:

此外,如果您想使用具有唯一名称的多个设置文件,您可以将它们放在 app/config 内的子目录中,如下所示:

app/config/yettings/first.yml
app/config/yettings/second.yml

Then you can access the values like this:

然后你可以访问这样的值:

FirstYetting.your_setting
SecondYetting.your_setting

It also provides you with default settings that can be overridden per environment. You can also use erb inside the yml file.

它还为您提供了可以在每个环境中覆盖的默认设置。您还可以在 yml 文件中使用 erb。