---
title: "Flash access changes in Rails 4.1"
description:
  "This post describes a flash object access changes due to new json cookie
  serialization in Rails 4.1"
canonical_url: "https://www.bigbinary.com/blog/flash-access-changes-rails-4-1"
markdown_url: "https://www.bigbinary.com/blog/flash-access-changes-rails-4-1.md"
---

# Flash access changes in Rails 4.1

This post describes a flash object access changes due to new json cookie
serialization in Rails 4.1

- Author: Vipul
- Published: July 24, 2014
- Categories: Rails

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.

```ruby

module FlashHelper
  ALERT_TYPES = [:success, :info, :warning, :danger]

  def bootstrap_flash
    flash_messages = []
    flash.each do |type, message|
      next if message.blank?

      type = :success if type == :notice
      type = :danger  if type == :alert
      type = :danger  if type == :error

      next unless ALERT_TYPES.include?(type)

      ....
      Array(message).each do |msg|
        text = content_tag(:div, msg.html_safe, :class => "alert fade in alert-#{type} ")
        flash_messages << text if msg
      end
    end
    flash_messages.join("\n").html_safe
  end
end

```

After upgrading to Rails 4.1, we started using the new
[Cookies serializer](http://guides.rubyonrails.org/upgrading_ruby_on_rails.html#cookies-serializer).
Following code was added to an initializer.

```ruby

  Rails.application.config.action_dispatch.cookies_serializer = :json

```

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](https://github.com/rails/rails/pull/13692) and
[this](https://github.com/rails/rails/pull/13945) 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](https://github.com/rails/rails/commit/a668beffd64106a1e1fedb71cc25eaaa11baf0c1)
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.

```ruby

flash["string"] = "a string"
flash[:symbol] = "a symbol"

# Rails < 4.1
flash.keys # => ["string", :symbol]

# Rails >= 4.1
flash.keys # => ["string", "symbol"]

```

## Solution

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.

```ruby

module BootstrapFlashHelper
  ALERT_TYPES = ['success', 'info', 'warning', 'danger']

  def bootstrap_flash
    flash_messages = []
    flash.each do |type, message|
      compare_type = type.to_s # Notice the stringifying of keys here, to make this work even with symbols.
      next if message.blank?

      compare_type = 'success' if compare_type == 'notice'
      compare_type = 'danger'  if compare_type == 'alert'
      compare_type = 'danger'  if compare_type == 'error'

      next unless ALERT_TYPES.include?(compare_type)

      Array(message).each do |msg|
        text = content_tag(:div, msg.html_safe, :class => "alert fade in alert-#{compare_type} ")
        flash_messages << text if msg
      end
    end
    flash_messages.join("\n").html_safe
  end
end

```

## Links

- [Human page](https://www.bigbinary.com/blog/flash-access-changes-rails-4-1)
