Prior to upgrading to Rails 4.1 we had a helper to display flash messages and to add css class to the message
based on flash type. Here is the code.
After upgrading to Rails 4.1, we started using the new Cookies serializer. Following code was added to an initializer.
Soon after this our flash helper started misbehaving and all flash messages disappeared from the application.
JSON Cookies Serializer
Before we move ahead, a word on the new JSON Cookies Serializer. Applications created before Rails 4.1 uses Marshal to serialize cookie values into the signed and encrypted cookie jars.
Commits like this and this
made it possible to have Cookies serializer and defaulted from Marshal Serializer to a secure Serializer using JSON.
The JSON Serializer works on JSON objects. Thus objects like Date and Time will be stored as strings. Hash keys will be stored as strings.
JSON serializer makes the application much safer since it is safer to pass around strings compare to passing around arbitrary values which is what was happens when values are marshalled and passed around.
Coming back to our problem, change Stringify the incoming hash in FlashHash coupled with above serialization changes meant that even if we put a symbol as a key in the flash we have to retrieve it as “string” since the keys are internally being converted into strings.
The difference is clearly illustrated below.
Now that we know the root cause of the problem the fix was simple. Instead of relying on symbols use “string” to access value from flash.
Share this article:
Subscribe to our monthly newsletter
Thank you! You have successfully subscribed to our newsletter.