レール >= 4.2
たとえば、ディレクトリにYAML
ファイルを作成するだけです。config/
config/neo4j.yml
の内容はneo4j.yml
次のようになります (簡単にするために、すべての環境でデフォルトを使用しました)。
default: &default
host: localhost
port: 7474
username: neo4j
password: root
development:
<<: *default
test:
<<: *default
production:
<<: *default
でconfig/application.rb
:
module MyApp
class Application < Rails::Application
config.neo4j = config_for(:neo4j)
end
end
これで、カスタム構成に次のようにアクセスできます。
Rails.configuration.neo4j['host'] #=>localhost
Rails.configuration.neo4j['port'] #=>7474
より詳しい情報
Rails の公式 API ドキュメントでは、config_for
メソッドを次のように説明しています。
現在の Rails 環境の config/foo.yml をロードするための便利さ。
yaml
ファイルを使いたくない場合
Railsの公式ガイドが言うように:
プロパティの下のカスタム構成を使用して、Rails 構成オブジェクトを使用して独自のコードを構成できますconfig.x
。
例
config.x.payment_processing.schedule = :daily
config.x.payment_processing.retries = 3
config.x.super_debugger = true
これらの構成ポイントは、構成オブジェクトを通じて利用できます。
Rails.configuration.x.payment_processing.schedule # => :daily
Rails.configuration.x.payment_processing.retries # => 3
Rails.configuration.x.super_debugger # => true
Rails.configuration.x.super_debugger.not_set # => nil
config_for
メソッドの公式リファレンス|
Rails公式ガイド