SEO through fixing Rails’ image_tag helper to show better alt text

It’s easy. Here’s how: create a config/initializers/asset_tag_helper.rb file and put this bit of code inside.

module ActionView
  module Helpers
    module AssetTagHelper
      def image_tag_with_alt source, options = Hash.new
        options[:alt] ||= File.basename source
        image_tag_without_alt source, options
      end
      alias_method_chain :image_tag, :alt
    end
  end
end

Restart your server. That’s it. Now, when you use image_tag, the alt text will show the image file name instead of cutting off the file extension.

Might even be good for SEO if your image files are named sensibly.


About this entry