text_field_with_auto_complete inside form_for

后端 未结 5 1761
既然无缘
既然无缘 2021-02-10 09:25

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_com

5条回答
  •  终归单人心
    2021-02-10 09:41

    If your intent is to set a id field, you must:

    1. render the results in LI items, each li having it DOM id set up to the id of the returned record, like this:

      • Item 1
      • < LI ID="2">Item 2< /LI>
    2. Your text field would be something like:

      <%= text_field_with_auto_complete :model, :method, {}, :after_update_element => ‘getSelectionId’ % >
      
    3. and a javascript method called getSelectionId like this

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

    note that you can do anything via javascript since you have the text and the li element available.

    Another option is this plugin

提交回复
热议问题