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.

Filed under  //  rails   ruby  
Posted

quick reminder for installing gems to heroku

1. create the file .gems in your app's root directory; with the gem name, version and source; for example:

mislav-will_paginate --version '>= 2.3.2' --source gems.github.com

 

2. add to git:

git add .gems

git commit -m "added gem manifest"

 

3. deploy:

git push heroku master

 

Filed under  //  git   heroku   ruby  
Posted