<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array"/>
  <modified type="array">
    <modified>
      <diff>@@ -105,13 +105,50 @@ module AutoCompleteMacrosHelper
   # auto_complete_for to respond the AJAX calls,
   # 
   def text_field_with_auto_complete(object, method, tag_options = {}, completion_options = {})
+    tag_id = tag_id_for(object, method, tag_options[:object])
     (completion_options[:skip_style] ? &quot;&quot; : auto_complete_stylesheet) +
     text_field(object, method, tag_options) +
-    content_tag(&quot;div&quot;, &quot;&quot;, :id =&gt; &quot;#{object}_#{method}_auto_complete&quot;, :class =&gt; &quot;auto_complete&quot;) +
-    auto_complete_field(&quot;#{object}_#{method}&quot;, { :url =&gt; { :action =&gt; &quot;auto_complete_for_#{object}_#{method}&quot; } }.update(completion_options))
+    content_tag(&quot;div&quot;, &quot;&quot;, :id =&gt; &quot;#{tag_id}_auto_complete&quot;, :class =&gt; &quot;auto_complete&quot;) +
+    auto_complete_field(&quot;#{tag_id}&quot;, { :url =&gt; { :action =&gt; &quot;auto_complete_for_#{tag_id}&quot; } }.update(completion_options))
   end
 
   private
+    def tag_id_for(object, method, object_instance)
+      object_name, method_name = object.to_s.dup, method.to_s.dup    
+      if object_name.sub!(/\[\]$/,&quot;&quot;)
+        if object_instance.respond_to?(:to_param)
+          auto_index = object_instance.to_param
+        else
+          raise ArgumentError, &quot;object[] naming but object param and @object var don't exist or don't respond to to_param: #{object.inspect}&quot;
+        end
+      end
+        
+      sanitized_object_name = sanitize_object_name(object_name)
+      sanitized_method_name = sanitize_method_name(method_name)
+      
+      if auto_index
+        tag_id_with_index(sanitized_object_name, sanitized_method_name, auto_index)
+      else
+        tag_id(sanitized_object_name, sanitized_method_name)
+      end
+    end
+  
+    def tag_id(object_name, method_name)
+      &quot;#{object_name}_#{method_name}&quot;
+    end
+
+    def tag_id_with_index(object_name, method_name, index)
+      &quot;#{object_name}_#{index}_#{method_name}&quot;
+    end
+
+    def sanitize_object_name(object_name)
+      object_name.gsub(/[^-a-zA-Z0-9:.]/, &quot;_&quot;).sub(/_$/, &quot;&quot;)
+    end
+
+    def sanitize_method_name(method_name)
+      method_name.sub(/\?$/,&quot;&quot;)
+    end
+  
     def auto_complete_stylesheet
       content_tag('style', &lt;&lt;-EOT, :type =&gt; Mime::CSS)
         div.auto_complete {
@@ -141,3 +178,12 @@ module AutoCompleteMacrosHelper
     end
 
 end   
+module ActionView
+  module Helpers
+    class FormBuilder
+      def text_field_with_auto_complete(method, tag_options = {}, completion_options = {})
+        @template.text_field_with_auto_complete(@object_name, method, objectify_options(tag_options), completion_options)
+      end
+	  end
+	end
+end
\ No newline at end of file</diff>
      <filename>lib/auto_complete_macros_helper.rb</filename>
    </modified>
    <modified>
      <diff>@@ -64,4 +64,10 @@ class AutoCompleteTest &lt; Test::Unit::TestCase
       text_field_with_auto_complete(:message, :recipient, {}, :skip_style =&gt; true)
   end
   
+  def test_fields_for_text_field_with_auto_complete
+    fields_for :message do |f| 
+      assert_dom_equal %(&lt;input id=\&quot;message_recipient\&quot; name=\&quot;message[recipient]\&quot; size=\&quot;30\&quot; type=\&quot;text\&quot; /&gt;&lt;div class=\&quot;auto_complete\&quot; id=\&quot;message_recipient_auto_complete\&quot;&gt;&lt;/div&gt;&lt;script type=\&quot;text/javascript\&quot;&gt;\n//&lt;![CDATA[\nvar message_recipient_auto_completer = new Ajax.Autocompleter('message_recipient', 'message_recipient_auto_complete', 'http://www.example.com/auto_complete_for_message_recipient', {})\n//]]&gt;\n&lt;/script&gt;),
+        f.text_field_with_auto_complete(:recipient, {}, :skip_style =&gt; true)
+    end
+  end
 end</diff>
      <filename>test/auto_complete_test.rb</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>7b18f05af68dec0f9e45b1a01d6b7c94e306caeb</id>
    </parent>
  </parents>
  <author>
    <name>Warren Seen</name>
    <email>warren@persephone.thematrix</email>
  </author>
  <url>http://github.com/warrenseen/auto_complete/commit/ad9f25805a4a82711aa3bd4d6a33546ab4e3563f</url>
  <id>ad9f25805a4a82711aa3bd4d6a33546ab4e3563f</id>
  <committed-date>2009-01-15T15:14:19-08:00</committed-date>
  <authored-date>2009-01-15T15:14:19-08:00</authored-date>
  <message>Updated auto_complete to work with form_for and fields_for.</message>
  <tree>7d04741a56c5c75580fc140e2f273d12c48fdad8</tree>
  <committer>
    <name>Warren Seen</name>
    <email>warren@persephone.thematrix</email>
  </committer>
</commit>
