Developer's Blog

Entries from May 2009

Using Rescue Statement When Outputting Data in ERB

Posted May 26, 2009 by Justin Lewis. Filed under Code.

Sometimes when you’re writing ERB, you’ll have to output something like this:

<%= article.category.name %>

But if the category is not set, you’ll probably get an error page with a nasty backtrace of an exception, along with an error like:

undefined method 'name' for nilclass

There are a couple ways to avoid this. First, you can test if category is nil before outputting the value:

<% if not article.category.blank? -%>
<%= article.category.name %>
<% else -%>
No Category
<% end -%>

Another way that is a bit cleaner is to add a rescue statement to the end of your output:

<%= article.category.name rescue 'No Category' %>

Using rescue will only catch exceptions, and will not test if the name itself is nil or an empty string. So, use either one based on your situation.

Add comment

Gaia Flash Framework

Posted May 06, 2009 by Justin Lewis. Filed under Tips.

Gaia is an open-source front-end Flash Framework for AS3 and AS2 designed to dramatically reduce development time.

Read more at gaiaflashframework.com

Add comment

Cufon Font Replacement

Posted May 06, 2009 by Justin Lewis. Filed under Tips.

A text replacement alternative to sIFR that requires no images or Flash, instead using Canvas and VML to create a similar effect. Read more on GitHub

Add comment