Sunday, January 24, 2010

Recipe 12: Custom Form Builder

(This post is one of a series that upadates Chad Fowler's Rails Recipes.)

What. Rails comes with a form builder but it is possible to override it and create one's own. This recipe shows how one can make a tabular form builder.

How. The main trick is to derive a class from ActionView::Helpers::FormBuilder in app/helpers/application_helper.rb. The derived class needs to override all the field helpers in the base class.

Anachronism. The meta-programming Chad Fowler recommends loops over the field helpers with some exceptions, i.e., the text includes the following:

(field_helpers - %w(check_box radio_button hidden_field)).each do

What has changed is that field_helpers now returns an array of symbols not strings. Thus, the line should now read:

(field_helpers - [:check_box, :radio_button, :hidden_field]).each do

No comments:

Post a Comment