I’m trying to implement autocomplete with special behaviour for a field in Rails 3.2. I’m using the rails autocomplete gem and what I want to do is:
There is a field in a form which I want the autocomplete to work on which contains qty and the type of qty for a recipe ingredient. Three different examples of an input:
100 gramms
2 pices
3.2 kilos
I want the behaviour to have the dropdown when the user presses space with all available options in the list. When the user enters another key the list would of course filter on that input.
Is it possible to customize the rails autocomplete for the above behaviour?
UPDATED. My original answer misunderstood the question.
Fisrt option.
You modify your controller action.
While the autocomplete behaviour will already be correct, this solution is suboptimal since it hits the server even while i am typing the quantity (and gives no results).
A complete solution involves patching the rails autocompleter driver.
Keep the abaove custom controller action and just put a custom minLength function
https://github.com/crowdint/rails3-jquery-autocomplete/blob/master/lib/assets/javascripts/autocomplete-rails-uncompressed.js#L63
you modify this section as:
now if you put this minlength filter to your field tag
it will correctly ignore the first bit (upto after the last white space) of the user input and only hits the server when you reach the first character of the unit. (little unsure of escapes here)
An advantage of this approach is that you could add ingredient autocompletion to your controller, and use comma and get multiple ingredients input simply by adding this option to autocomplete text field helper:
Then you can properly autocomplete inputs like: