The If Works This dirt was a building before

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")
  end

end

Now the following call:

<%= pencil_icon %>

produces this image tag:

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

1 Comment

This is what makes Ruby so inspiring to use.

But…

In this example, I think a method would be better. By doing this, you can avoid the hackish-ness of your approach.

Or?

Posted by Mikkel on 9 June 2008 @ 3pm

Leave a Comment