Search code examples
ruby-on-railsyamlerror-messaging

Should I use an YAML file or the DB to store my success/error messages in Rails app?


In my Rails app, after executing some code I want to send Slack messages to users to notify them of the execution result. There are multiple processes for which I want to send these messages, and in short, I need somewhere to store message templates for successes/errors (they're just short strings, like "Hi, we've successfully done x!", but they differ for each process).

Right now, I have a SlackMessage model in the DB from which you can retrieve the message content. However, I heard that it's better to manage custom messages like this in a yml file, since it's easier to add/edit the messages later on (like this, even though this is for locales).

What is the best practice for this kind of scenario? If it's not to use a DB, I appreciate if you could give pointers or a link on how to do it (in terms of using yml files, the only material I could find was on internationalisation).


Solution

  • Why don't you use the already existing I18n module in Rails? This is perfect for storing messages, and gives you the ability to use translations would you ever need them in the future.

    Getting a message is simple:

    Slack.message(I18n.t(:slack_message, scope:'slack'))
    

    In this case you need a translation file like this:

    en:
      slack:
        slack_message: This is the message you are going to select.
    

    Read more on I18n: https://guides.rubyonrails.org/i18n.html