Home > RUBY > Which is the best way to store human message and config in your rails app?

Which is the best way to store human message and config in your rails app?

September 23, 2008

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).

  1. Create a YAML file inside config, say settings.yml which contains a hash over there, then load it from config/initializers say for example load_setting.rb. Here’s the code for settings.yml :

    :flash:
      :user:
        :created: 'Thanks for signing up!'
    
      :job:
        :updated: 'Thanks for updating your job!'
    		

    So that we can load that settings.yml inside config/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?

  2. Create a file inside model or lib which contains a Hash, then load it from controller for example.
  3. 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?

update , 20091116

rails 2.3 got nice config initializers locales en yaml file for this.