views:

1783

answers:

4

I need to create one config option for my Rails application. It can be the same for all environments. I found that if I set it in environment.rb, it's available in my views, which is exactly what I want...

environment.rb

AUDIOCAST_URI_FORMAT = http://blablalba/blabbitybla/yadda

Works great.

However, I'm a little uneasy. Is this a good way to do it? Is there a way that's more hip?

A: 

That was the topic of another question a few days ago...

pantulis
+15  A: 

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:

APP_CONFIG = YAML.load_file("#{RAILS_ROOT}/config/config.yml")[RAILS_ENV]

You can then retrieve the value using:

uri_format = APP_CONFIG['audiocast_uri_format']

See this Railscast for full details.

John Topley
That's really cool.
Ethan
A: 

I like simpleconfig. It allows you to have per environment configuration.

Jerry Cheung
A: 

see my response to http://stackoverflow.com/questions/566333/where-is-the-best-place-to-store-application-parameters-database-file-code/1697933#1697933

A variation to what you had in that it's a simple reference to another file. It sees that environment.rb isn't constantly updated and doesn't have a heap of app specific stuff in it. Though not a specific answer to your question of 'is it the Rails way?', perhaps there'll be some discussion there about that.

Straff