Taming Rails Conditionals for Noobs: Or with Case
Sometimes you gotta wrestle some view into shape and the database might be part of the problem. Anyway, you end up in googleville or rails forums and don your hunting hat.
I needed to check for nulls, nils and plain old blanks to format my output nicely so I thought I'd end up with something like this:
<% case @person.address_2 when !nil, !'' %><%=h @person.address_2 %>,<% end %>
which doesn't throw an error, but it also doesn't really output properly, what does is:
<% if !@person.address_2.blank? %><%=h @person.address_2 %>,<% end %>
This tasty snippet allowed me to check for not *empty* before outputting an address section with a lovely comma (I didn't want a nasty looking string of commas when I didn't have an address).
As ever, they'll be a best practice ruby way but this worked for me, for now.