Flash messages in Rails applications need a bit of love. A lot of them tend to be identical within a given app, and it’s a bit annoying to manage defaults and fallback for all these calls to
I18n.t(). The following is a small helper-method and a simple structure to keep it all neatly organized.
For this to work, add the text you want shown to your
config/locale/en.yml (by default):
posts#create action has a custom flash for the
:created flash-message. All other controllers and actions in the application where a flash-message of
:created is used the default message of
"Successfully created" will be shown.
Next, add this to your
ApplicationController (this is all of the magic):
Finally, this is how you use it within an action:
Try to keep the actions somewhat generic like
:updated, etc. This makes the default values easier to manage.
What just happened?
I18n system has some neat convenience shortcuts we’re using here. First of all we build the primary flash lookup-key based on the current controller and action.
Second bit of functionality used here is the fallback message. Fallback messages can be given as an explicit string, or a symbol. When a symbol is used it will be used to lookup a new message in the
I18n backend. We use that for the
:default argument where the fallback is set to be a global flash message like
Now you can customize all the flashes in your app without touching the code, you have all these small bits of user interface in a single file for easy overview. You can also easily vary the messages between languages, customize text where it is necessary, and have a convenient global fallback message.