Search

Enter a search word or two and press return to see the search results.

Who am I?

Hi, I’m Graeme and these are my notes, from my messy desk. I started this blog because Google proved to be more useful at finding content than anything else I’ve used.

So I started adding my own content in the hopes that Google would index it and allow me to find things again in the future.

It works.

You can find out more about me here, and you should follow me on Twitter here.

Keeping up

You can automatically receive new content here by subscribing to the “Blog RSS” (link below). This is the easiest way to keep up with what I write here.  See this BBC article for a good introduction on RSS and keeping up with the goings on of the Internet more easily.

« The Databox | Main | Using the shell within irb »
Thursday
13Jul2006

Tabular FormBuilder for easily creating forms in Rails

OK, HTML forms. There's a bit of a nightmare. How do you style them so that they are semantically correct, but look good on all modern browsers? I've tried in the past, and I've failed pretty miserably. There's a couple of options out there:

* [Prettier Accessible Forms](http://alistapart.com/articles/prettyaccessibleforms)
* [Awesome Forms](http://paularmstrongdesigns.com/examples/css/awesome-form.html)

but they have their quirks and disadvantages too. So I decided to be irritatingly pragmatic for now and use tables. I know, it's not *right*, but it's good enough, OK?

So, I created a form builder to produce them. Actually I first created it back in May, but I've finally gotten around to using it in anger, so it's been tidied up a little, and a few features added. 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.

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

{:multipart => true}) do |f| %>
true %>
true %>
'Longer description' %>



Notice how easy it was to create a form there, that will be styled consistently with all your other forms in your application? Nice. :-) 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, so tough :-P ). 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. :-)

PrintView Printer Friendly Version

EmailEmail Article to Friend

Reader Comments (10)

I made a little change to add some date convenience methods:

module Rubaidh
module TabularForm
class TabularFormBuilder < ActionView::Helpers::FormBuilder
(field_helpers - %w(check_box radio_button) %w(date_select datetime_select)).each do |selector|
...

Pretty dirty, but quite handy. There's probably a better way to figure out which methods to add here. :)

Thanks for your plugin!

October 27, 2006 | Unregistered CommenterEric O'Connell

On the latest Rails prerelease, this plugin will fail to load in development mode after the first request (seem like something to do with reloading of plugins). They aren't supposed to reload at all anymore. Any ideas? Cheers!

January 2, 2007 | Unregistered CommenterJoshua Sierles

I have the same problem, not loading after the first request. I am looking into it right now.

January 28, 2007 | Unregistered CommenterKris

I worked around the problem by directly including the plugin in my application helper for now.

January 28, 2007 | Unregistered CommenterKris

Joshua, the problem is that the init.rb file of the plugin extends ApplicationHelper, instead it should extend ActionView::Base. Just a simple change to get this working again, should be backwards compatible aswell.

January 30, 2007 | Unregistered CommenterTarmo Tänav

I tried that workaround by Tarmo but the plugin is still broken.

July 6, 2007 | Unregistered CommenterEdwin

Tarmo: Thanks for pointing out the fix. I've now incorporated it in svn.

Y'all might want to look at http://svn.rubaidh.com/plugins/trunk/yet_another_form_builder which, thanks to a good friend & CSS guru, now appears to lay out forms nicely with CSS. Score! At some point, I'll document it and release it properly...

July 6, 2007 | Unregistered CommenterGraeme Mathieson

Hm looks nice. Is this the best there is for rails 2.0? I'm new ... how do I use it,


  1. which one? the yet_another_form_builder or the svn link above?

I tried both and both gave a 500, it says tabular_form_for not found

January 19, 2008 | Unregistered CommenterFormer

I like aurita-gui better. It's a real form generator, not just some views helper. Just look at the examples, i love it.

http://aggrgator.com/doc/aurita-gui

sudo gem install aurita-gui

It's the best form builder implementation for ruby i ran across.
There's no view helper for aurita-gui (it's plain ruby, no rails plugin), but this should be trivial.

January 14, 2009 | Unregistered Commenterosmodeo

The plugins can now be found here

git://github.com/rubaidh/form_builders.git

and here

http://github.com/rubaidh/form_builders

with info here

http://rubaidh.com/portfolio/open-source/form-builder/

March 9, 2009 | Unregistered CommenterKalin Harvey

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
Post:
 
Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>