Navigation

Home > Projects > Tabular Form Builder

Tabular Form Builder

In our applications, we build a lot of HTML forms. It can be awkward to make sure that they are all correctly laid out, and consistent. To make life easier, there's a new feature in Rails 1.1: form builders. I've created a custom form builder which uses tables to lay out forms. OK, so this might not be the most semantic use of XHTML, but it's the most pragmatic way to create forms because it Just Works on most browsers, rather than the nightmare of creating forms with CSS. I thought I'd share this form builder with other people as a plugin.

Installation

You can grab a copy from my plugin repository with the following magick juju:

script/plugin install http://svn.rubaidh.com/plugins/trunk/form_builders

Add -x to make it an svn:externals and keep up to date with the latest version.

Usage

To make it work, you'd do something along the lines of:

<% tabular_form_for('photograph', @photograph, :html => {:multipart => true}) do |f| %>
  <%= f.text_field 'title', :required => true %>
  <%= f.file_column_field 'file', :required => true %>
  <%= f.text_area 'description', :label => 'Longer description' %>
  <%= f.submit "Add" %>
<% end %>

Notice how easy it was to create a form there, that will be styled consistently with all your other forms in your application? I've added a couple of extra options. If you pass :required => true into a field, it'll stick a '*' next to the field (and in true convention-over-configuration form, that's hard-coded). By default it will create a label by humanising the field name, but you can always pass in something more friendly with :label => 'Friendly field label'.

That's about it, really. Dead simple. As usual, I'm open to feedback on improving it. And patches. Especially patches, in fact. :-)