how/where to declare a global data structure?

17 views
Skip to first unread message

Ed

unread,
Mar 4, 2008, 2:38:48 PM3/4/08
to Ruby on Rails: Talk
I'd like to read a custom config file into a hash at rails startup and
have access to this hash from my controller actions. I wrote a sub to
create the hash in my application controller (wrong place?) but I'm
having trouble accessing the hash from my actions. I'm probably doing
something silly as I'm fairly new to RoR.

Thanks a lot.

Stefan Lang

unread,
Mar 4, 2008, 4:35:18 PM3/4/08
to rubyonra...@googlegroups.com
2008/3/4, Ed <pyr...@gmail.com>:

Assuming it's a yaml file, completely untested:

require "yaml"

class ApplicationController < ActionController::Base

MY_CONFIG = YAML.load(File.read(File.join(RAILS_ROOT, "config",
"my_config.yml")))

end

Now you can simply access MY_CONFIG from all your controllers.

Mark Bush

unread,
Mar 4, 2008, 2:49:11 PM3/4/08
to rubyonra...@googlegroups.com

The application controller seems the right place to put the method to
read in the data. For it to be available to all controllers in their
methods, you probably want to put it into a class variable...

--
Posted via http://www.ruby-forum.com/.

Reply all
Reply to author
Forward
0 new messages