Using Initializers

I have a template that I use for creating Rails web sites. There is some limited template configuration data which I store in a global hash variable. This hash variable is initialized in the applications controller. It all works just fine. However, I think that I need to clean up my act a bit. The applications controller is probably not the best place to initialize the global variable. I tried to do it with an assignment statement in an initializer file; but, it didn't work (i.e., view templates didn't see it for some reason). Can someone please tell me why that is and what might work? Also, I fully recognize that global variables are to be avoided (but it sure works fine :slight_smile: ). Does anyone have a suggestion as to what I might use to replace the global variable?

Thanks for any input.

         ... doug

Interesting. The procedure that you describe is much different than what I expected. So, I guess that I'm not surprised that what I did failed to work. Thanks. I'll give it a try.

          ... doug

I have another example that I have used for a while...

/config/settings.yml all: &all_environment_settings   constant_for_all: somethinf

development: &non_production_settings     <<: *all_environment_settings     something_different_for_dev: dev stuff

Then you can load this file in a initializer:

/initializers/settings.rb SETTINGS = YAML.load_file("#{RAILS_ROOT}/config/settings.yml") [RAILS_ENV].symbolize_keys

Now you can call this through SETTINGS[:constant_for_all]

Both methods will work but if you have requirements for the constants to change between environments then this would be better...

Thanks to all who responded to my inquiry.

This appears to be a rather basic issue that, for whatever reason, doesn't seem to get much attention.

I've reviewed everything. My conclusion is that, for my purposes, the very basic first approach of just declaring the single needed global constant in a file added to the initializers directory is enough to meet my needs.

Again, thanks to all for the input.

       ... doug

Doug Jolley wrote:

Thanks to all who responded to my inquiry.

This appears to be a rather basic issue that, for whatever reason, doesn't seem to get much attention.

I've reviewed everything. My conclusion is that, for my purposes, the very basic first approach of just declaring the single needed global constant in a file added to the initializers directory is enough to meet my needs.

I hope you don't mean "global" as in "Ruby global with a dollar sign". If so, that's asking for trouble. Please see Railscast #85 for a simple way of doing this right.

Again, thanks to all for the input.

       ... doug

Best,