How to define custom configuration variables in Rails?
Update 1
Very recommended: I'm going with Rails Config gem nowadays for the fine grained control it provides.
Update2
If you want a quick solution, then check Jack Pratt's answer below.
Although my original answer below still works, this answer is now outdated. I recommend looking at updates 1 and 2.
Original Answer:
For a quick solution, watching the "YAML Configuration File" screen cast by Ryan Bates should be very helpful.
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
Custom rails configuration section
What you're looking for is ActiveSupport::OrderedOptions.
It is used by rails internally to set config namespace. It allows to do something like this, in config/application.rb
:
module MyApp
class Application < Rails::Application
# ...
config.foo = ActiveSupport::OrderedOptions.new
config.foo.bar = :bar
end
end
You can then retrieve it the usual way :
Rails.configuration.foo.bar
If you want this to be environment specific, you also can use it in config/environments/*.rb
rather than config/application.rb
.
Best way to create custom config options for my Rails app?
For general application configuration that doesn't need to be stored in a database table, I like to create a config.yml
file within the config directory. For your example, it might look like this:
defaults: &defaults
audiocast_uri_format: http://blablalba/blabbitybla/yadda
development:
<<: *defaults
test:
<<: *defaults
production:
<<: *defaults
This configuration file gets loaded from a custom initializer in config/initializers:
# Rails 2
APP_CONFIG = YAML.load_file("#{RAILS_ROOT}/config/config.yml")[RAILS_ENV]
# Rails 3+
APP_CONFIG = YAML.load_file(Rails.root.join('config/config.yml'))[Rails.env]
If you're using Rails 3, ensure you don't accidentally add a leading slash to your relative config path.
You can then retrieve the value using:
uri_format = APP_CONFIG['audiocast_uri_format']
See this Railscast for full details.
Rails - Custom config in environment config files?
I just use constants. Eg
AWS_PW = "ssss"
you can have different values in the different config files
Access them by their name. They're constants. They're available everywhere--controllers, views, models, etc
# eg
user.pw = AWS_PW
ADDED
Constants need to start with an upper case letter. Usual practice is to use all upper case, underscores, numerals, etc.
You need to restart Rails to pick up the new changes to the environment files since the appropriate environment file is read once.
Note: you can declare a hash as a constant. Eg
# in an environment file...
PARAMS = {}
PARAMS['default_pw'] = 'topsecret!'
Rails custom configuration returns an empty hash
I'm guessing the following:
- You have the Spring gem bundled in.
- Your custom configuration somehow got initialized in the state it is currently in. (i.e. empty)
- The
config\prefs.yml
isn't tracked by Spring, so it doesn't know the environment needs to be reloaded.
If i'm correct, you'll just have to create an initializer with the following code:
Spring.watch "config/prefs.yml"
And, of course, you'll have to reload the console each time the config is changed. I've managed to reproduce and solve your issue with this, so i hope this helps.
Related Topics
Ruby - Create Singleton with Parameters
Active Admin: Sorting on Multiple Columns
Are There Any Ruby Orms Which Use Cursors or Smart Fetch
Rails 12 Hour Am/Pm Range for a Day
Sunspot_Rails Gem - " Errno:: Econnrefused (Connection Refused - Connect (2)) "
Discriminate First and Last Element in Each
How to Calculate 32 Bit Crc in Ruby on Rails
Ruby: How to Find Out If a Character Is a Letter or a Digit
Env: Ruby\R: No Such File or Directory
Pretty Print to a File in Ruby
Start Using Ruby on Rails, Web Services and Oauth
Ruby String with Usd "Money" Converted to Number
How to Enable Compression in Ruby on Rails
How to Implement Options Hashes in Ruby