Wolfmans Howlings

A programmers Blog about Programming solutions and a few other issues

Having multiple text_field_with_auto_complete in the same view

Posted by Jim Morris on Tue Oct 17 23:45:47 -0700 2006

I ran into a problem using text_field_with_auto_complete in a view where I wanted to have many of them created by an iteration. You can use the :index option for the text_field, but it doesn't carry over to the various divs used in the AJAX calls.

is what you really want to do, just as for a regular text_field.

I googled around and found this bug report for a fix to text_field_with_auto_complete that allows :index, as that fix does not appear to be in the current stable release of Rails or on Edge rails, I just created a my_text_field_with_auto_complete and it worked like a charm!!

So I put this in app/helpers/application_helper.rb and simply use my_text_field_with_auto_complete in my views.

Here is what the controller side looks like...

Posted in Rails  |  Tags rails,text_field_with_auto_complete  |  18 comments

Comments

  1. Kevin Olbrich said on Wed Oct 18 04:36:18 -0700 2006
    Nice work. One problem I see. When the parameters get serialized, you end up with something like {"object"=>{"index"=>"value"}} in the params, which confuses the standard `auto_complete_for handler`.
  2. Kevin Olbrich said on Wed Oct 18 04:50:16 -0700 2006
    Whoops, no. Actually it does work.
  3. wolfman said on Wed Oct 18 09:31:17 -0700 2006
    Good point I'll add the controller side of the code too, as it does work.
  4. kevin hutchison said on Sun Oct 29 11:12:06 -0800 2006
    Can i ask how the responder knows which partial to render? Or does it render all name partials?

    thanks much,h
  5. wolfman said on Sun Oct 29 13:08:54 -0800 2006
    kevin- it will render the partial called `_names.rhtml` in the relevant views directory. There is only one partial used in this case. Maybe I misunderstood your question?
  6. ThirtyTwo said on Tue Oct 31 05:24:41 -0800 2006
    Well done. Thank you.
  7. arun agrawal said on Wed Jun 27 07:10:27 -0700 2007
    thanks
  8. arun agrawal said on Wed Jun 27 07:15:33 -0700 2007
    i need a default value in text box when i came that page. and when i just delete the value manually and type another then the auto text will apppear
  9. Bala said on Thu Jul 12 04:29:39 -0700 2007
    I Tried this example i couldnt able to list the details but i can able to retrieve the datas what would be the problem? plz help me guyz, one of the requirements to my project.
  10. wolfmanjm said on Thu Jul 12 16:31:33 -0700 2007
    Can you email me the details? I don't really understand what problem you are having.
  11. Bala said on Fri Jul 13 00:34:25 -0700 2007
    # Controller.

      def user_auto_complete
        name= params[:user].keys[0] # get index as its always only one at a time
        responder_auto_complete_for_user_name(params[:user][name][:name])
      end



      private
      def responder_auto_complete_for_user_name(value)
        param= '%' + value.downcase + '%'
        find_options= {
          :conditions => [ 'name like ?', param ], :order => 'name ASC', :limit => 60
        }
        @names = User.find(:all, find_options)
        render :partial => 'names'
        #render :update do |page|
          #page['user_1_name_auto_complete'].show
          #page['user_1_name_auto_complete'].replace :partial => "names"
        #end
      end


    # Application helper



    module ApplicationHelper


      def my_text_field_with_auto_complete(object, method, tag_options = {}, completion_options = {})
        if(tag_options[:index])
          tag_name = "#{object}_#{tag_options[:index]}_#{method}"
        else
          tag_name = "#{object}_#{method}"
        end
        puts "Tag Name = > " + tag_name
        (completion_options[:skip_style] ? "" : auto_complete_stylesheet) +
            text_field(object, method, tag_options) +
            content_tag("div", "", :id => tag_name + "_auto_complete", :class => "auto_complete") +
            auto_complete_field(tag_name, { :url => { :action => "auto_complete_for_#{object}_#{method}" } }.update(completion_options))
      end

      def auto_complete_text_one(object, method, tag_options = {}, completion_options = {})
        if(tag_options[:index])
          tag_name = "#{object}_#{tag_options[:index]}_#{method}"
        else
          tag_name = "#{object}_#{method}"
        end
        (completion_options[:skip_style] ? "" : auto_complete_stylesheet) +
        text_field(object, method, tag_options) +
        content_tag("div", "", :id => tag_name + "_auto_complete", :class => "auto_complete") +
        auto_complete_field(tag_name, { :url => { :action => "user_auto_complete" } }.update(completion_options))
      end


      def auto_complete_text_two(object, method, tag_options = {}, completion_options = {})
        if(tag_options[:index])
          tag_name = "#{object}_#{tag_options[:index]}_#{method}"
        else
          tag_name = "#{object}_#{method}"
        end
        puts tag_name
        (completion_options[:skip_style] ? "" : auto_complete_stylesheet) +
        text_field(object, method, tag_options) +
        content_tag("div", "", :id => tag_name + "_auto_complete", :class => "auto_complete") +
        auto_complete_field(tag_name, { :url => { :action => "auto_complete_for_" + tag_name } }.update(completion_options))
      end

      end


    # view file



    <% 1.upto(3) do |legi| %>
    <%=auto_complete_text_one( :user, :name, {:index => legi, :size => 20}) %><br/><br/><br/><br/><br/>
    <% end %>


    # Database design

    The database contains a table called 'Users' and the fieldname is 'name'.
  12. Bala said on Fri Jul 13 00:34:47 -0700 2007
    Can you find the probs on above code
  13. wolfmanjm said on Fri Jul 13 09:03:59 -0700 2007
    The code in my article works fine, I use it. If you are having problems, then send me more information on what problems you are having, or send me your code. The email address is at the top of this page on the right.
  14. Curtis said on Sun Jul 15 21:08:57 -0700 2007
    What does your partial look like?
  15. Curtis said on Sun Jul 15 21:11:40 -0700 2007
    Maybe this?
     
          `<ul>`
            <% @names.each do|name|%>
        <li><%= name %></li>
            <%end%>
          `</ul>`
     
  16. wolfmanjm said on Sun Jul 15 22:23:55 -0700 2007
    The partial I usually use is somewhat more complex as I like to include the ID. However there is a default used which looks much like what you show above, and for the purposes of this article should be sufficient.
  17. julien said on Mon Dec 03 18:11:30 -0800 2007
    Just wanted to say thanks for this! It solved my problem completely (I had the same issue with autogenerated fields) and it works like a charm. This should replace the standard text_field_with_autocomplete for future releases of RoR
  18. Jared said on Fri Mar 07 18:43:53 -0800 2008
    Just thought you know this has been fixed:

    `http://dev.rubyonrails.org/attachment/ticket/5983/add_index_support_to_text_field_with_auto_complete.diff`

(leave email »)