Method: RecordSelectHelper#record_select_autocomplete

Defined in:
lib/record_select/helpers/record_select_helper.rb

#record_select_autocomplete(name, current, options = {}) ⇒ Object

Adds a RecordSelect-based form field. The field is autocompleted.

Arguments

name

the input name that will be used to submit the selected value.

current

the current object. provide a new record if there’re none currently selected and you have not passed the optional :controller argument.

Options

controller

The controller configured to provide the result set. Optional if you have standard resource controllers (e.g. UsersController for the User model), in which case the controller will be inferred from the class of current (the second argument)

params

A hash of extra URL parameters

id

The id to use for the input. Defaults based on the input’s name.



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/record_select/helpers/record_select_helper.rb', line 68

def record_select_autocomplete(name, current, options = {})
  options[:controller] ||= current.class.to_s.pluralize.underscore
  options[:params] ||= {}
  options[:id] ||= name.gsub(/[\[\]]/, '_')
  options[:class] ||= ''
  options[:class] << ' recordselect'

  ActiveSupport::Deprecation.warn 'onchange option is deprecated. Bind recordselect:change event instead.' if options[:onchange]

  controller = assert_controller_responds(options.delete(:controller))
  params = options.delete(:params)
  record_select_options = {}
  if current and not current.new_record?
    record_select_options[:label] = label_for_field(current, controller)
  end

  html = text_field_tag(name, nil, options.merge(:autocomplete => 'off', :onfocus => "this.focused=true", :onblur => "this.focused=false"))
  url = url_for({:action => :browse, :controller => controller.controller_path, :escape => false}.merge(params))
  html << javascript_tag("new RecordSelect.Autocomplete(#{options[:id].to_json}, #{url.to_json}, #{record_select_options.to_json});")

  return html
end