Thursday, July 30, 2009

How to handle uninitialized constants in Rails

I had this in my code.

klass = Object.const_get(params[:controller].singularize.camelize)

Which was working fine until I had a controller that doesn't have a matching model. Eg. Home

NameError: uninitialized constant Home

To handle this I needed to do a rescue to the error

rescue NameError
return true

No comments:

Blog Archive