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" />






One Response to “Quick and dirty icons in ActionView”

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?

Mikkel added these pithy words on Jun 09 08 at 3:40 pm

Leave a Reply