Which is the best way to store human message and config in your rails app?
Where do you save your flash[:notice] howaa your record has been successfully created, updated, blah blah, deleted, etc ? Where do you save your config file ? YAML ? Hash ? Other ways.
Here is a list of ways I know, you can share yours, you can just leave your comment (no worries don’t be afraid to speak up as I don’t bite).
I actually spoke about this at Indonesian Ruby mailing list around a month ago (in Bahasa Indonesia).
-
Create a YAML file inside config, say
settings.ymlwhich contains a hash over there, then load it fromconfig/initializerssay for exampleload_setting.rb. Here’s the code forsettings.yml::flash: :user: :created: 'Thanks for signing up!' :job: :updated: 'Thanks for updating your job!'So that we can load that
settings.ymlinsideconfig/initializers/load_setting.rb:SETTINGS = YAML.load_file("#{RAILS_ROOT}/config/settings.yml")Then we can call that flash inside our controller like this
flash[:notice] = SETTINGS[:flash][:user][:created]
From my experience, my designer and creative director who worked with me loved this, he can handle all those human and marketing language without being afraid to touch and mess up my codes.
Its weakness is if we use ADD (asshole driven development) oops I mean coding then go to browser refresh got error back again to coding then this yaml file is not automatically loaded by the web server. Yuck, then don’t use ADD, use XP!
What do you say about this?
- Create a file inside model or lib which contains a Hash, then load it from controller for example.
- Create a plugin which contain a mattr_accessor that provide a attr_accessor for Mixin, lurk at restful-authentication for this idea.
So what do you think about this?