Making Valid XHTML Easier

I’m working on a Rails site in my Copious Free Time and I wanted to share a little way that Ruby made my life easier. I’m making my pages valid XHTML 1.0 Transitional because it makes life easier to find bugs and it just feels good to know I’m meeting the spec.

The W3C Validator complained that I didn’t have the rows and cols attributes on my <textarea> tags. My code for them looked like:

<%= text_area_tag :message, params[:message] %>

And I don’t want to add the :size option because I use CSS to style all of them, it’d be confusing to see an unused size there. So I extended the text_area_tag method in my app/helpers/application_helper.rb to fill in a default:

module ApplicationHelper
  def text_area_tag(name, content=nil, options={})
    super(name, content, { :size => "40x10" }.update(options))

And it was that easy to start having valid tags. I’ll be posting more Rails snippets and tips as I work on my project, and I’ll definitely announce here when it’s ready for wide release.

(This snippet owes thanks to ‘leethal’ in #rubyonrails on for straightening out how I was trying to override the method.)

Want more? I'm not as good at forgetting to update @pushcx on Twitter.