rails:freeze:gems | freezzo.com

November 28, 2007

You have a nil object when you didn’t expect it!

Filed under: Ruby on Rails — Tags: , , , , — Randy @ 6:44 am

I found this cool little snippet of code somewhere on the internet. Anywhere you access an object in a view, adding the following will ensure the page doesn’t blow up:

 <%= @object.association.variable rescue nil %>

Of course, you won’t know if you have an error now either.

Powered by WordPress