Rails 4 Custom Flash Type Undefined After Error

A suggested fix to https://github.com/rails/rails/issues/12057

After an error is raised the custom flash methods are unavailable, it seems that they disappear from the scope of from where they were defined.

The class instance variable which holds the name of the custom flash types persists but it does not redefine the method upon a new request (perhaps exceptions clear out the scope of the dynamically inserted methods of a controller?).

The suggested fix checks whether the controller has the new method and if it doesn't it define_method it.

I was not able to reproduce the error in the integration test while imitating a route that raises an exception so I added a unit test.