Local Business Reviews Application on a Ruby on Rails Platform

Ruby on Rails Tutorials & Code Examples

Extend rails image_tag to use ALT as TITLE for seo

This code snippet will make code like:
<%= image_tag "rails.png", :alt => 'rails logo' %>

output (with a title tag):
< alt="rails logo" src="/images/rails.png?1182293075">title="rails logo" />

Whack the following somewhere (eg. you could test it by sticking it at the end of environment.rb):

# Extend img tag to use the alt text for the title attribute if alt is present & title is empty (for SEO)
module ActionView::Helpers::AssetTagHelper
alias_method :orig_image_tag, :image_tag
def image_tag(source, options = {})
if options[:alt] && !options[:title]
options[:title] = options[:alt]
end
return orig_image_tag(source, options)
end
end

Labels: , , , ,