Quick and dirty icons in ActionView

Tiny tiny Rails tip: you can use method_missing to make using icon sets easier when writing Rails views. Behold:

module ApplicationHelper

  def method_missing(sym, *args)
    super unless sym.to_s =~ /_icon$/
    image_tag("silk/#{sym.to_s.gsub(/_icon$/, '')}.png")


Now the following call:

<%= pencil_icon %>

produces this image tag:

<img src="/images/silk/pencil.png" />

If you’ve enjoyed this article, you might enjoy my recently published book JavaScript Testing Recipes. It’s full of simple techniques for writing modular, maintainable JavaScript apps in the browser and on the server.