The World of Mayukh Bose

<< Back to Main Page Mayukh's world: e-mail me | about
Mayukh's World: Filtering text_field_with_auto_complete in Rails Saturday, February 16, 2019
  • Background
  • The Problem
  • Coding the View
  • Coding the Controller
  • Credits
  • My Free Software
  • Delphi/C++ Builder
  • Pocket PC
  • FreeSpeech Chat
  • C/C++ Freebies
  • Perl
  • Python
  • Ruby
  • My Ruby Code
  • Filtering with auto_complete
  • Migrating Rails
  • Ruby Tutorials and Software e-mail me

    Coding the Controller

    As mentioned in the previous page, we've modified our view so that when it makes the XMLHttpRequest, it calls our controller with two parameters instead of one. Next we need to modify our controller to read the two parameters and do our own custom processing. As before, the changed lines are marked in bold font. First thing we do is delete the following line in the controller:
       auto_complete_for :category, :short_desc <--- DELETE THIS LINE
    and replace it with the following short subroutine:
      def auto_complete_for_category_short_desc 
        auto_complete_responder_for_categories params[:man_id], params[:category][:short_desc]
    This change allows us to read both parameters that are passed by the XMLHttpRequest and then call our own custom drop-down population code with these parameters.

    Next we go to the bottom of our controller and add a private method:
      def auto_complete_responder_for_categories(man_id, cat_name)
        if cat_name.length < 3
          @categories = []
        elsif man_id.to_i == 0 
          @categories = Category.find(
                                      :conditions => ['c.short_desc LIKE ?', '%' + cat_name + '%'],
                                      :order => 'short_desc'
          @categories = Category.find(
                                      :conditions => ['c.short_desc LIKE ? AND man_id = ?', '%' + cat_name + '%', man_id],
                                      :order => 'short_desc'
        render :inline => "<%= auto_complete_result @categories, 'short_desc' %>"
    That sure is a lot of code. Let's examine what all this code does.

    It is important to note is that this is a private method. We don't want this method to be available to the general public as a best programming practice. Note that this function accepts two arguments, the man_id (which is the value of the select field) and cat_name (which is the part of the string typed in the edit box).

    First thing we do is check the length of the cat_name argument and set @categories to a blank array if the length is less than 3 characters long. This was an optimization I did, since I didn't want to query the database if the typed in text was too short. You can remove this if block if you like.

    The next if block checks to see if the man_id == 0. This is the value of the select box if the user has selected the '-- all manufacturers --' option. In this case, we should not use the man_id in our SQL statement. Hence, we set @categories using a SQL query that does not consider man_id. Note that if you want to query multiple database tables, you could use find_by_sql() instead of find().

    Finally the else block is called IF the cat_name is at least 3 characters long AND the select box is set to a particular manufacturer (rather than -- all manufacturers --). In this case, we want to populate the drop down based on both the selection in the select box, as well as what is already typed in the text box. Note that the SQL statement in the else block uses both the parameters in its where clause. As noted before, you could use find_by_sql() instead of find(), especially if you need to query multiple database tables for your results.

    Finally we need to populate the drop down that shows up under the edit box. To do this, we call the render() function and tell it to populate the drop-down with the contents of @categories and use the short_desc field from the @categories array. Note the interesting argument passed to the :inline argument -- it contains some rails code!

    That's all there is to it. Now your auto-complete edit box will respect settings of other form elements as well and can filter its auto-complete results accordingly.

    All that remains to be done is to give credits and props to the following people.

    <<Previous: Coding the View ^Up to Mayukh's World^ Next: Credits >>

    Copyright © 2004 Mayukh Bose. All rights reserved.
    This work may be freely reproduced provided this copyright notice is preserved.
    OPTIONAL: Please consider linking to this website ( as well.