简体   繁体   中英

Rails: configuring applications

I am reading and coding along with a tutorial.

I have an application.yml file with some constants created to hold data. To include those constants in ENV and initialize them at start, this code was given:

config_file = Rails.application.config_for(:application) #this is the bothersome part
config_file.each do |key,value|
ENV[key] = value
end unless config_file.nil? 

I fail to fully understand this code. In particular, on the first line, where do the chained objects come from what do they mean and how do I create such on my own?

It loads data from a config file into the app. The example from the docs about #config_for :

#config/app.yml:
production:
  url: http://127.0.0.1:8080
  namespace: my_app_production
development:
  url: http://localhost:3001
  namespace: my_app_development

If you do rails c and type Rails.application.config_for(:app) into the console, you'll get:

 {"url"=>"http://localhost:3001", "namespace"=>"my_app_development"} 

which is just a regular hash you can loop through using #each or access it's values through keys.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM