Rietta.com Security logo
You are reading The Rietta Blog, a publication about the web since 2005.

Default HTML Values With a Rails View Helper

Suppose you have the same image tag included frequently in a Rails application. One way to clean up the code a bit and to set defaults is to create a super simple view helper method that can receive a hash parameter and merge it with the defaults.

Code Example

In app/helpers/pages_helper.rb:

def gui_tiny_icon(name, opts = {})
  image_tag "gui/theme/icons/#{name}",
    {:class => "vm", width: 15, height: 15}.merge(opts)

Then in the usual place of the HTML.ERB template, one can now do this:

  <%= gui_tiny_icon "twitter.png", alt: "Frank's Twitter" %> 
  <%= gui_tiny_icon "linkedin.png", alt: "Frank's LinkedIn" %>
  <%= gui_tiny_icon "twitter.png", alt: "Bob's Twitter" %>

About Frank Rietta

Frank Rietta's photo

Frank Rietta is a web application security architect, author, and speaker. He is a computer scientist with a Masters in Information Security from the College of Computing at the Georgia Institute of Technology. He speaks about security topics and was a contributor to the security chapter of the 7th edition of the "Fundamentals of Database Systems" textbook published by Addison-Wesley.

If there is a topic you would like us to cover,