I have a fixed fix for unwanted spaces from the image. I added :after_update_element => "trimSelectedItem" to the model_auto_completer parameter model_auto_completer (the first hash of the three data). My trimSelectedItem then finds the corresponding subitem and uses its contents for the value of the element:
function trimSelectedItem(element, value, hiddenField, modelID) { var span = value.down('span.display-text') console.log(span) var text = span.innerText || span.textContent console.log(text) element.value = text }
However, this starts with the option :allow_free_text , which by default changes the text back as soon as the text field loses focus if the text inside is not a "valid" element from the list. Therefore, I also had to disable this by passing :allow_free_text => true to the parameter hash (again, the first hash). I would prefer this to continue.
So my current call to create autocomplete is:
<%= model_auto_completer( "line_items_info[][name]", "", "line_items_info[][id]", "", {:url => formatted_products_path(:js), :after_update_element => "trimSelectedItem", :allow_free_text => true}, {:class => 'product-selector'}, {:method => 'GET', :param_name => 'q'}) %>
And products /index.js.erb:
<ul class='products'> <%- for product in @products -%> <li id="<%= dom_id(product) %>"> <%= image_tag image_product_path(product), :alt => "" %> <span class='display-text'><%=h product.name %></span> </li> <%- end -%> </ul>
source share