开发者

Need to load contents of XML file into a constant variable, when to do this?

开发者 https://www.devze.com 2023-02-10 19:24 出处:网络
I want to load a xml file into a collection, and I will need to access this on each and every开发者_开发百科 page request.

I want to load a xml file into a collection, and I will need to access this on each and every开发者_开发百科 page request.

I only need to load it once, where/what point should I do this?


You can create an initializer to load the xml file and put into a constant. config/initializers/load_xml_collection.rb


The most straight-forward way would be to set it up as an initializer. Create a new file in #{Rails.root}/config/initializers called load_xml_file.rb (or something a little more descriptive)

Then, within that, you could do something along the lines of:

SETTINGS_FROM_XML_FILE = method_to_read_xml

This will be executed once when your app is loaded. You'll also be able to access SETTINGS_FROM_XML_FILE anywhere in your application.

The only caveat is that if the file changes, you'll need to re-start the application, or come up with a more sophisticated way of loading the details you need.

0

精彩评论

暂无评论...
验证码 换一张
取 消