Rails auto complete with form_for and fields_for support

I’ve forked the Rails auto_complete plugin to add support for the following usage:

  1. <% form_for(@foo) do |f| %>
  2.   <%= f.text_field_with_auto_complete :bar %>
  3. <% end %>

This fork also supports nested fields_for calls:

  1. <% form_for(@foo) do |f| %>
  2.   <% f.fields_for(@foo.baz) do |baz_f| %>
  3.     <%= baz_f.text_field_with_auto_complete :quux %>
  4.   <% end %>   
  5. <% end %>

You can find my fork on Github, here.

4 Responses to “Rails auto complete with form_for and fields_for support”

  1. Pat Says:

    Hi Warren,

    I just noticed this week that we both forked the auto_complete plugin recently to do this; you can read about my take on it here:

    http://patshaughnessy.net/repeated_auto_complete

    This seems like an obvious missing feature or bug with auto_complete; have you tried submitting a patch to the original rails/auto_complete version?

  2. Carl Says:

    Wow. I love the internet. Two solutions to my problem. Unfortunately neither worked. Yours came the closest. Primarily because your api matches up with the way Rails does things better.

    The problem yours has is in auto_complete_macros_helper.rb when auto_index is null (when there isn’t a value) it calls tag_id which doesn’t match the id being generated. I removed that last if statement in #tag_id_for and everything worked like a champ.

    Mine is working but more than likely I am doing it wrong, so could you publish some example code using the plugin?

    Thanks!!

  3. warren Says:

    Pat,

    I haven’t had a chance to really work it out and see if there are any further bugs.

    Carl,

    Like this one. Thanks for the heads up, I’d not come across that in my limited testing (ie it works for me).

    I’ll take a look and post an update soon.

  4. Thomas VOD and visioconference Says:

    I am having strange problems about routing. I already have an auto_complete field that works, but adding a new one will make Rails fail to recognize the route. Any idea?

Leave a Reply