I don't use Rails but this is how I did it in Python using UUIDs:
# set flash messages like this
def flash(self, title, body):
session['flash_messages'].append({
'title': title,
'body': body,
'uuid': uuid().hex # stores a UUID as a string
})
...
self.flash('foo', 'bar')
Then in the base template I have this:
<script type="text/javascript">
{% for m in session.flash_messages %}
if(!Cookies.get('{{m.uuid}}')) {
Notify('{{m.title}}', '{{m.body}}');
Cookie.set('{{m.uuid}}', 'true', 86400); // key, value, expiry seconds
}
{% endfor %}
</script>
I'll break it down for the Pythonically-challenged:
- When you add a flash message, you create a unique ID and store it with that message.
- Before you display the message, you check to see if a cookie named with the message's unique ID has been set.
- If that cookie has not been set, flash the message and set the cookie. Expire the cookie in a day, or as brief as you think is wise.
Now if this page is pulled from cache, it will be okay. At step 2 the test for the cookie will pass because it has already been set, and the message will not be displayed.