Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

text_field_with_auto_complete inside form_for

Simple question really - how do I use text_field_with_auto_complete inside a form_for block?

I've tried doing f.text_field_with_auto_complete but that gives an error, and just using text_field_with_auto_complete by itself doesn't seem to do anything.

Am I missing something here?

like image 557
robintw Avatar asked Dec 23 '22 14:12

robintw


2 Answers

I personally use this:

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

And add the auto-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;
}
like image 171
Can Berk Güder Avatar answered Feb 09 '23 08:02

Can Berk Güder


A couple of months back 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 support text fields that appear more than once in a form by generating unique ids to allow the javascript to work, but it might help solve your problem also, or at least give you some new ideas.

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

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

like image 20
pat Avatar answered Feb 09 '23 09:02

pat