Rails 4 FlashHash Upgrade Gotcha

We upgraded an application from Rails 3 to Rails 4 this week and came across an interesting gotcha which I haven’t seen documented anywhere.

As we rolled out the Rails 4 version of the app we split the traffic between the upgraded Rails 4 app and our existing Rails 3 app. Some of the requests to Rails 3 app failed with the following exception:

Production NoMethodError: undefined method `sweep' for {"discard"=>[], "flashes"=>{"just_switched"=>true}}:Hash

Error Message: NoMethodError: undefined method `sweep' for {"discard"=>[], "flashes"=>{"just_switched"=>true}}:Hash

Where:
[GEM_ROOT]/gems/actionpack-3.2.19/lib/action_dispatch/middleware/flash.rb, line 239

It turns out that Rails 4 serialise the flash to the cookie differently to Rails 3. When Rails 3 attempts to deserialise it you get the above error. This error can also occur between Rails 3 and Sinatra apps.

To get around the problem whilst in the migration period we patched the Hash class. This would mean of course that the flash methods wouldn’t work, but as they are only used for minor presentation tweaks that seemed a good compromise.

Creating an application form style layout using Prawn

I used the excellent Prawn gem recently to create a PDF. I needed an application form style like layout for the PDF like this:

Simple form layout

Simple form layout

Using Prawn this is straight forward. We just create two bounding boxes one for the left side and one for the right side of the page. Then we render the labels into the left bounding box and the form boxes into the right bounding box.

Filling in the fields

filled-in-form

Filled in form

In some case I need to fill in the fields as well so I generalised the code into a method that takes a hash:

Programming language stereotypes venn diagram

Inspired by Blame it on the voices’
Religions venn diagram of the day, I put together a programming language stereotypes venn diagram. Stereotypes are supplied by the Google instant search results for the questions: why is language? and why is language so?:
I drew the venn diagram together using a little processing.js script which was pretty straightforward and fun.

  var circleSize = 300;
  void setup()
  {
    background(255)
    size(900, 600)
    textAlign(CENTER);
    stroke(204, 102, 0);
    smooth();
  }

  void draw()
  {
    noFill();
    ellipse(400, 200, circleSize, circleSize)
    ellipse(300, 400, circleSize, circleSize);
    ellipse(500, 400, circleSize, circleSize);

    ellipse(800, 200, 150, 150);

    language = createFont("Helvetica",12,true);
    textFont(language);
    textSize(24);

    fill(0, 0, 0);
    text("Lisp", 800, 100)
    text("Java", 400, 30)
    text("Ruby", 100, 400)
    text("Javascript", 720, 400)

    textSize(20);
    text("Great", 800, 180)
    text("Used for AI", 800, 220)

    text("slow", 400, 330)

    text("hard", 400, 170)
    text("always updating", 400, 210)

    text("popular", 330, 300)
    text("red", 280, 400)

    text("used", 470, 300)

    text("bad", 520, 380)
    text("important", 520, 420)

  }