Method: RecordSelectHelper#record_select_field
- Defined in:
- lib/record_select/helpers/record_select_helper.rb
#record_select_field(name, current, options = {}) ⇒ Object
Adds a RecordSelect-based form field. The field submits the record’s id using a hidden input.
Arguments
name-
the input name that will be used to submit the selected record’s id.
current-
the currently selected 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.
field_name-
The name to use for the text input. Defaults to ”, so field is not submitted.
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/record_select/helpers/record_select_helper.rb', line 33 def record_select_field(name, current, = {}) [:controller] ||= current.class.to_s.pluralize.underscore [:params] ||= {} [:id] ||= name.gsub(/[\[\]]/, '_') [:class] ||= '' [:class] << ' recordselect' ActiveSupport::Deprecation.warn 'onchange option is deprecated. Bind recordselect:change event instead.' if [:onchange] controller = assert_controller_responds(.delete(:controller)) params = .delete(:params) = {} [:field_name] = .delete(:field_name) if [:field_name] if current and not current.new_record? [:id] = current.id [:label] = label_for_field(current, controller) end html = text_field_tag(name, nil, .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.Single(#{[:id].to_json}, #{url.to_json}, #{.to_json});") return html end |