12 Oct
Several months ago, I began using Michael Ivey’s merb_has_flash gem on Guestlist. The gem, which provides Rails-like flash accessors to Merb, works as advertised, until I wanted to manipulate the flash contents via JavaScript. The use case: setting a success message and then reloading the page, all client-side. It was difficult because, although the Gem stores the flash contents in the user’s cookies, it’s stored as a Ruby serialized hash that seems unreadable via JavaScript.
The solution? A fork called merb_has_json_flash, which instead of serializing the flash in Ruby, does so using JSON. Now you can set the success message in Ruby or JavaScript, and the behaviour is identical. The only downside is that the flash store is no longer obfuscated, so it’s user-readable – not a big deal.
Here’s an example, using the jQuery cookie plugin:
<script type="text/javascript">
$.cookie('flash', JSON.stringify({
notice: 'Ticket successfully saved.'
}), { path: '/' }
);
window.location.reload();
</script>
Anyways, that’s it. I hope someone else finds this useful – although it seems like Merb users are far and few between these days.