Rietta
Rietta: Web Apps Where Security Matters
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:

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

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

1
2
3
  <%= 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 consultant, software developer, 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 teaches about security topics and is a contributor to the security chapter of the 7th edition of the "Fundamentals of Database Systems" textbook published by Addison-Wesley.