Simplified JQuery Autcomplete solution for multiple fields
===

i just made my first successful integration of jquery's autocomplete, to return and filter results from the mysql database in my rails 3 app as you type into a number of text fields in a couple different forms for different tables.

it works tremendously, but is definitely an inefficient code setup. i spent some time trying to write it the correct way, but i just couldn't get it right; a little too far beyond my current skill level.

**i thought i'd post the solution here for those newbies still looking (as even this was a bit difficult for me), and to see if anyone could recommend/guide us thru some tips for simplifying it properly?**

(this is also my first SO contribution; i finally felt i had something worthy, so please excuse any newbie mistakes)


**in my gemfile;**

    group :assets do
         gem 'jquery-ui-rails'
    end
    
    gem 'jquery-rails'

then make sure all gems are installed.

**in my application.css file, which is included in the necessary views;**

 

    *= require jquery.ui.all

**in the associated controller file for one form;**

      # GET /auto_custom_method1.json
      def auto_custom_method1
        render :json => Model.where("database_column_name1 LIKE ?", "%#{params[:term]}%").map(&:database_column_name1).uniq
      end
     
      # GET /auto_custom_method2.json
      def auto_custom_method2
        render :json => Model.where("database_column_name2 LIKE ?", "%#{params[:term]}%").map(&:database_column_name2).uniq
      end
     
      # GET /auto_custom_method3.json
      def auto_custom_method3
        render :json => Model.where("database_column_name3 LIKE ?", "%#{params[:term]}%").map(&:database_column_name3).uniq
      end
     
      # GET /auto_custom_method4.json
      def auto_custom_method4
        render :json => Model.where("database_column_name4 LIKE ?", "%#{params[:term]}%").map(&:database_column_name4).uniq
      end
     
      # GET /auto_custom_method5.json
      def auto_custom_method5
        render :json => Model.where("database_column_name5 LIKE ?", "%#{params[:term]}%").map(&:database_column_name5).uniq
      end


**in the associated controller file for another form;**

  

    # GET /auto_custom_method6.json
      def auto_custom_method6
        render :json => Model.where("database_column_name5 LIKE ?", "%#{params[:term]}%").map(&:database_column_name6).uniq
      end
     
      # GET /auto_custom_method7.json
      def auto_custom_method7
        render :json => Model.where("database_column_name5 LIKE ?", "%#{params[:term]}%").map(&:database_column_name7).uniq
      end
     
      # GET /auto_custom_method8.json
      def auto_custom_method8
        render :json => Model.where("database_column_name5 LIKE ?", "%#{params[:term]}%").map(&:database_column_name8).uniq
      end

**in my application.js file, which is included in the necessary views;**

model_name, auto_custom_method, and #field_id are all customized according to your unique application.

    //= require query
    //= require jquery_ujs
    //= require jquery.ui.all
    
    $(function() {
        $.getJSON("/model_name1/auto_custom_method1", function(data) {
            $("#field_id1").autocomplete({
                source: data,
                minLength: 1
            });
        });
    });
    $(function() {
        $.getJSON("/model_name1/auto_custom_method2", function(data) {
            $("#field_id2").autocomplete({
                source: data,
                minLength: 1
            });
        });
    });
    $(function() {
        $.getJSON("/model_name1/auto_custom_method3", function(data) {
            $("#field_id3").autocomplete({
                source: data,
                minLength: 1
            });
        });
    });
    $(function() {
        $.getJSON("/model_name1/auto_custom_method4", function(data) {
            $("#field_id4").autocomplete({
                source: data,
                minLength: 1
            });
        });
    });
    $(function() {
        $.getJSON("/model_name1/auto_custom_method5", function(data) {
            $("#field_id5").autocomplete({
                source: data,
                minLength: 1
            });
        });
    });
    $(function() {
        $.getJSON("/model_name2/auto_custom_method6", function(data) {
            $("#field_id6").autocomplete({
                source: data,
                minLength: 1
            });
        });
    });
    $(function() {
        $.getJSON("/model_name2/auto_custom_method7", function(data) {
            $("#field_id7").autocomplete({
                source: data,
                minLength: 1
            });
        });
    });
    $(function() {
        $.getJSON("/model_name2/auto_custom_method8", function(data) {
            $("#field_id8").autocomplete({
                source: data,
                minLength: 1
            });
        });
    });

