Module: ActionView::Helpers::FormHelper

Defined in:
lib/autosuggest/form_helper.rb

Instance Method Summary collapse

Instance Method Details

#autosuggest_field(object_name, method, source, options = {}) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/autosuggest/form_helper.rb', line 4

def autosuggest_field(object_name, method, source, options={})
  text_field_class = "autosuggest_#{object_name}_#{method}"
  options[:class] = "#{options[:class].to_s} #{text_field_class}"
  autosuggest_options = options.delete(:autosuggest_options) || {}
  autosuggest_options.reverse_merge!("queryParam" => "query", "selectedItemProp" => "name", "searchObjProps" => "name", "neverSubmit" => "true", "asHtmlName" => "#{object_name}[set_#{method}]")

  _out = text_field(object_name, method, options)

  _out << raw(%{
    <script type="text/javascript">
      $(document).ready(function(){
        // removing name attribute since values will be returned in #{object_name}[set_#{method}]
        $('.#{text_field_class}').autoSuggest('#{source}', #{autosuggest_options.to_json})
                                 .removeAttr('name');
      });
    </script>
  })
  _out
end