# Complex forms + DHH's auto\_complete plugin = works!

**URL:** <https://www.ruby-forum.com/t/complex-forms-dhhs-auto-complete-plugin-works/157371>\
**Category:** Rails\
**Created:** [January 10, 2009, 3:22am UTC](https://www.ruby-forum.com/t/complex-forms-dhhs-auto-complete-plugin-works/157371 "2009-01-10T03:22:30Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![alyoshka](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/b19c9b/32.png) [@alyoshka](https://www.ruby-forum.com/u/alyoshka)\
**Post date:** [January 10, 2009, 3:22am UTC](https://www.ruby-forum.com/t/complex-forms-dhhs-auto-complete-plugin-works/157371/1 "2009-01-10T03:22:30Z")

</div>

I’ve been searching for a way to get DHH’s auto\_complete plugin to  
work with Ryan B.’ complex forms. All I could find was people  
asking the same question, but no solutions so I decided to try it  
myself. I don’t know if this id the best way to do it, but the  
solution was to monkey patch ActionView::FormHelper and FormBuilder  
which I put in a seperate file in the /lib folder of my project.  
Here’s the code:

## lib/auto\_complete\_form\_helper.rb

module ActionView  
module Helpers  
module FormHelper  
def text\_field\_with\_auto\_complete\_mod(object, method,  
tag\_options = {}, completion\_options = {})  
sanitized\_object = object.gsub(/][|[^-a-zA-Z0-9:.]/, “_").sub  
(/_$/, “”)  
sanitized\_method = method.to\_s.sub(/?$/,”")  
(completion\_options[:skip\_style] ? “” :  
auto\_complete\_stylesheet) +  
text\_field(object, method, tag\_options) +  
content\_tag(“div”, “”, :id =\> “#{sanitized\_object}_#  
{sanitized\_method}auto\_complete", :class =\> “auto\_complete”) +  
auto\_complete\_field("#{sanitized\_object}#{sanitized\_method}",  
{ :url =\> { :action =\> "auto\_complete\_for_#{sanitized\_object}\_#  
{sanitized\_method}” } }.update(completion\_options))  
end  
end

```
class FormBuilder
  def text_field_with_auto_complete(method, tag_options = {},

```

completion\_options = {})  
@template.text\_field\_with\_auto\_complete\_mod(@object\_name,  
method,  
objectify\_options(tag\_options), completion\_options)  
end  
end  
end  
end

Don’t forget to include the file in your environment.rb of course. I  
know some people were looking for a way to get this to work, so I  
thought it might be helpful if I post it on the mailing list. If you  
see any bugs or know of a better way, please let me know.

---

<div class="post-metadata">

**Author:** ![alyoshka](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/b19c9b/32.png) [@alyoshka](https://www.ruby-forum.com/u/alyoshka)\
**Post date:** [January 10, 2009, 3:24am UTC](https://www.ruby-forum.com/t/complex-forms-dhhs-auto-complete-plugin-works/157371/2 "2009-01-10T03:24:15Z")

</div>

Ooh, that code isn’t too readable. Um, I posted it on my website as  
well: [http://detyabozhye.com/](http://detyabozhye.com/)

---

<div class="post-metadata">

**Author:** ![alyoshka](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/b19c9b/32.png) [@alyoshka](https://www.ruby-forum.com/u/alyoshka)\
**Post date:** [March 12, 2009, 9:56pm UTC](https://www.ruby-forum.com/t/complex-forms-dhhs-auto-complete-plugin-works/157371/3 "2009-03-12T21:56:10Z")

</div>

Hmm, maybe we could some parts together. I like the one in the second  
link, but looking at the code, I think there’s more extra in there. I  
would still need to check it out more thoroughly.

One thing I would like to do is extend the autocomplete functionality  
to autocomplete for a whole object.

eg: If you have an Address model which contains street, city, region,  
zip. In a form you start typing the street and the autocomplete popup  
comes up with the full address. When you choose the address, it  
automatically fills in all the fields for the address. Has anybody  
done that yet and is it possible to do it easily with Prototype?

---

<div class="post-metadata">

**Author:** ![alyoshka](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/b19c9b/32.png) [@alyoshka](https://www.ruby-forum.com/u/alyoshka)\
**Post date:** [February 6, 2009, 1:57pm UTC](https://www.ruby-forum.com/t/complex-forms-dhhs-auto-complete-plugin-works/157371/4 "2009-02-06T13:57:55Z")

</div>

Hi… I wrote some similar code, and packaged it up into a plugin.  
See: [The auto\_complete plugin refactored to support repeated fields and named scopes - Pat Shaughnessy](http://patshaughnessy.net/repeated_auto_complete)

I also know there are a few other forks of the auto\_complete plugin  
that also solve this same problem, for example:  
[http://warrenseen.com/blog/2009/01/16/rails-auto-complete-with-form\_for-and-fields\_for-support/](http://warrenseen.com/blog/2009/01/16/rails-auto-complete-with-form_for-and-fields_for-support/)

Or just look through the auto\_complete forks on github:

> **[Forks · rails/auto\_complete](https://github.com/rails/auto_complete/network/members)**
>
> NOTICE: official repository moved to https://github.com/david-kerins/auto\_complete - Forks · rails/auto\_complete

Hope this helps, - pat
