Quick Zebra Striping in Rails

Posted by admin 12/08/2007 at 08h57

Striping table rows is usually a necessity when making accessible web pages. This can be a tedious task. Through the ages (and by ages I mean the last 10 years or so) there have been many ways to do this in many dynamic languages. Everything from taking the modulo of the index when iterating through the set, to flipping a counter back and forth.

Well, if you’re programming in rails, you get a much easier way to do it via the cycle method. Let me show you how easy it is:

  <% @objects.each do |obj| %>
  <tr class="<%= cycle("even", "odd") %>">
    <td><%= obj.id %></td>
    <td><%= obj.title %></td>
  <% end %>

Simplicity itself. Now as you loop through the set, it will cycle the class name of the row between “even” and “odd”. Now all you need to do is set CSS values for those classes and your done.

Here is a quick example of a CSS class for odd:


Gotta love rails.


Rescuing Ruby

Posted by admin 11/08/2007 at 19h07

Now, before anyone goes crazy, this article has nothing to do with saving ruby from something, i just couldnt think of a better title. We will be talking about rescue and some cool tricks that can be done with it.

As most of you vets know, every thing in Ruby is an object. What most noobs dont realize is how powerful that can be and how concise your code can become. Of course, the flip side of that problem is what i call, newbie-come-expert. It’s basically the stage in a noob’s study of a language where they think they know enough to write really cool software. What actually happens is they end up with code like this:

@objects.map {|e| [e.id, e.title]}.sort {|a, b| a.last <=> b.last}.map {|e| e.join(" ")}.join("\n")


Dont worry noobs, everyone does it. I’m still trying to undo it from some of my older code.

Anyway, my point is, sometimes Ruby’s dynamicism (say that 3 times fast) can lead to messy code so use this tip wisely.

On to the actual tip.

So, like I said before, EVERYTHING in Ruby is an object, even the remains of a rescue clause or an inline rescue which is what we will be playing with here.

How many of you have done this:

@person = Person.find(some_id)

only to get the following error:

ActiveRecord::RecordNotFound: Couldn't find Person with ID=22

It’s pretty annoying, mainly because this type of thing could easily happen in a live app. A user goes to find something that isnt there. One way around it is to have a rescue at the end of the method, but what if you have multiple finds that could throw errors? What if you still need to be able to proceed with whatever you are doing, even if the object isnt found?

This is where inline @rescue@’s can help.

Now you can pull all of your finds together and end them with a rescue.

Let’s say you had the following code:

@person = Person.find(params[:id])
@employee = Employee.find(params[:employee_id])

Now, if either of these finds fail, you will get an error. Instead of rescuing the entire method, you can resuce each statement and make the return value nil and check for nil like you normally would.

New code:

@person = Person.find(params[:id]) rescue nil
@employee = Employee.find(params[:employee_id]) rescue nil

Now when you cant find the ID, it will rescue and just set the variables to nil and move on.

You can also set the rescues to another default datatype instead of nil.

Now, you may be saying, “Wait, why would a user need to enter an id”. Well, one example is an order id, or a tracking number. Anyway, I have run across this a couple of times and thought i’d write something about it.

until next time.


Flvorful has launched!

Posted by admin 11/08/2007 at 18h06

Just starting the blog with a quick note about what we do. Basically, we deliver commercials into flash videos. Not just pre-roll, but also in the video. We connect the advertiser to the publisher through our online marketplace and some fancy algorithms :)

We will be putting up some examples of it soon. In the mean time you can check out how it works.