With the "server side solution" you can just pass your extra params through the path helper like this and the autocomplete jquery plugin will append them to the Ajax request:
This autocomplete data source can be put in a separate generic autocomplete action instead of the index action and be introduced to the client through a view helper:
resources :categoriesdo
collection do
get :autocompleteendend
This module about a generic autocomplete action can be included at the top of every resource controller based on a ActiveRecord::Base child class (you have to set the autocomplete route for each controller as shown above):
The association setters could be generated dynamically through meta programming.
So the following code generates special setter methods for all belongs_to and polymorphic associations when included in an ActiveRecord::Base child class.
ruby
moduleAssociationSetter
extend ActiveSupport::Concern
included do
columns.map(&:name).select{|c| c.match('_id')}.each do |column|
association = column.split('_id').first.classify
define_method "#{association.underscore}_name"doself.send(association.underscore).try(:name)
end
accessible_attributes << "#{association.underscore}_name"
define_method "#{association.underscore}_name="do |name|
returnif name.blank?
association_type = association
ifself.class.columns.map(&:name).include?("#{association.underscore}_type")
association_type = self.send("#{association.underscore}_type")
endself.send("#{association.underscore}=", association_type.constantize.find_or_initialize_by_name(name))
endendendend
P.S.: Maybe it turns out to be worth to extract in a gem some day ;-)
The carrierwave-direct gem implements the key methods through calling mount_uploader in the painting model.
UPDATE: You have to wrap the following code around the columns loop to get it working with migrations:
With the "server side solution" you can just pass your extra params through the path helper like this and the autocomplete jquery plugin will append them to the Ajax request:
_form.html.erb
<%= f.text_field :category_name, data: {autocomplete_source: categories_path(your_extra_param: 'value')} %>
Great screencast. It helped me to DRY this problem a little bit up for SIMPLE model constellations as mentioned in this screencast.
Enclosed you'll just find an untested refactoring version but an older similar refactoring version has been tested successfully ;-)
So I would generally transform all tags with a data-autocomplete-source attribute into an autocomplete input.
application.js.coffee
This autocomplete data source can be put in a separate generic autocomplete action instead of the index action and be introduced to the client through a view helper:
application_helper.erb
_form.html.erb
<%= autocomplete_input(f, :category) %>
routes.rb
This module about a generic autocomplete action can be included at the top of every resource controller based on a ActiveRecord::Base child class (you have to set the autocomplete route for each controller as shown above):
The association setters could be generated dynamically through meta programming.
So the following code generates special setter methods for all belongs_to and polymorphic associations when included in an ActiveRecord::Base child class.
P.S.: Maybe it turns out to be worth to extract in a gem some day ;-)