1 minutes estimated reading time.
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)
end
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" %>