Using AutoComplete with ActiveScaffold forms

Posted by anup.narkhede on July 01, 2009

This is a quick guide to set up auto_complete text fields in forms rendered by ActiveScaffold plugin.
I am not a huge fan of ActiveScaffold, but found these steps worth publishing after doing this for one typical project requirement.

1. Models

2. Install ActiveScaffold and AutoComplete Plugins

3. Controller Configuration

This renders a drop down list for country field, instead of the default create/replace form. However, we need to render an auto completing text box for which we need to replace the form column.

4. Form column override

Next step is to override the form column. Create a file named _country_form_column.html.erb in app/views/cities folder.

The second option :method => :get is needed to avoid the InvalidAuthenticityToken error generated by the ajax post request.

5. Since the form now returns country[name] instead of record[country_id] in params, we need to assign country object before creating city record. The CitiesController finally looks like: