Text_field_with_auto_complete inside form_for

A simple question: how to use text_field_with_auto_complete inside the form_for block?

I tried to do f.text_field_with_auto_complete , but that gives an error, and just using text_field_with_auto_complete alone does nothing.

Did I miss something?

+4
source share
5 answers

A few months ago I wrote a plugin that allows you to call the " text_field_with_auto_complete " method directly on fields_for or form_for . The idea was to maintain text fields that appear several times in the form, generating unique identifiers that allow javascript to work, but this can also help solve your problem or at least give you some new ideas.

See: http://github.com/pat11639/repeated_auto_complete

... and this article for details on how it works: http://patshaughnessy.net/2008/10/31/modifying-the-autocomplete-plugin-to-allow-repeated-fields

+3
source

I personally use this:

 <%= f.text_field :name, :autocomplete => "off" %><div class="auto_complete" id="customer_name_auto_complete"></div> 

And add the automatically generated Javascript and CSS manually. Here's the javascript:

 new Ajax.Autocompleter('customer_name', 'customer_name_auto_complete', '/customers/auto_complete_for_customer_name', { method:'get', paramName:'customer[name]', minChars: 3 }); 

and here's the CSS:

 div.auto_complete { width: 350px; background: #fff; } div.auto_complete ul { border:1px solid #888; margin:0; padding:0; width:100%; list-style-type:none; } div.auto_complete ul li { margin:0; padding:3px; } div.auto_complete ul li.selected { background-color: #ffb; } div.auto_complete ul strong.highlight { color: #800; margin:0; padding:0; } 
+6
source

If you intend to set the id field, you must:

  • displays the results in LI elements, each of which has a DOM identifier set for the identifier of the returned record, for example:

     <UL> <LI ID="1">Item 1</LI> < LI ID="2">Item 2< /LI> </UL> 
  • Your text box will look something like this:

     <%= text_field_with_auto_complete :model, :method, {}, :after_update_element => 'getSelectionId' % > 
  • and a javascript method called getSelectionId, like this

     function getSelectionId(text, li) { alert (li.id); } 

note that you can do anything through javascript since you have text and a li element available.

Another option is this plugin

+1
source

Even inside the form_for block, you must specify the complete model, because it is used to build the Ajax callback. Also in the empty hash contributed by Ricardo, you can transfer additional options to the autocomplete field, including ": token", etc.

+1
source

I had this problem too, but the solution I used may not apply to you.

Since the object that I requested in autocomplete belonged to a different model, I used fields for this that solved the problem of autocomplete.

 <% fields_for :model, @formobject.model do %> <p> <%= text_field_with_auto_complete :object, :field, :skip_style => true %> </p> <% end %> 

Good luck

0
source

All Articles