Method: RecordSelectHelper#record_multi_select_field

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

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

Adds a RecordSelect-based form field for multiple selections. The values submit using a list of hidden inputs.

Arguments

name

the input name that will be used to submit the selected records’ ids. empty brackets will be appended to the name.

current

pass a collection of existing associated records

Options

controller

The controller configured to provide the result set.

params

A hash of extra URL parameters

id

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



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/record_select/helpers/record_select_helper.rb', line 118

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

  controller = assert_controller_responds(options.delete(:controller))
  params = options.delete(:params)
  record_select_options = {}
  record_select_options[:current] = current.inject([]) { |memo, record| memo.push({:id => record.id, :label => label_for_field(record, controller)}) }

  html = text_field_tag("#{name}[]", nil, options.merge(:autocomplete => 'off', :onfocus => "this.focused=true", :onblur => "this.focused=false"))
  html << ('ul', '', :class => 'record-select-list');

  # js identifier so we can talk to it.
  widget = "rs_%s" % name.gsub(/[\[\]]/, '_').chomp('_')
  url = url_for({:action => :browse, :controller => controller.controller_path, :escape => false}.merge(params))
  html << javascript_tag("#{widget} = new RecordSelect.Multiple(#{options[:id].to_json}, #{url.to_json}, #{record_select_options.to_json});")

  return html
end