adding errors to an autocomplete field and ensuring it is wrapped with fieldWithErrors

So I'm failing to find how to do this the 'rails-way' Here's the problem, it's easier to show it in an example...but basically I'm having issues with validation errors highlighting (wrapping) with the 'fieldWithErrors' div and multiple models in a belongs_to relationship. So here's the example: models:    child.rb       belongs_to :parent       validates_presence_of :parent       validates_presence_of :name

   parent.rb       has_many :children

controller:   # basic scaffold style controller with an method to support ajax:   indexed_auto_complete_for :child, :parent, :name

view:   <label for="parent_name">parent</label><br/>   <%= text_field_with_auto_complete :parent, :name,                                     { :value => @parent_name },                                     { :skip_style => true,                                       :after_update_element => 'auto_complete_on_select' } %>   <% # hidden field is updated via ajax %>   <%= hidden_field :child, :parent_id %>

modified lib/auto_complete.rb     #     def indexed_auto_complete_for(parent, object, method, options = {})       define_method("auto_complete_for_#{object}_#{method}") do         find_options = {           :conditions => [ "LOWER(#{method}) LIKE ?", '%' + params[object][method].downcase + '%' ],           :order => "#{method} ASC",           :limit => 10 }.merge!(options)

        @items = object.to_s.camelize.constantize.find(:all, find_options)

        render :inline => "<%= indexed_auto_complete_result @items, '#{parent}_#{object}_id', '#{method}', 'id' %>"       end     end

modified application.js // this methods are to allow autocomplete on :belongs_to a bit easier, requires: // - application.js - auto_complete_on_select // - auto_complete_macros.rb - indexed_auto_complete_result // - auto_complete.rb - indexed_auto_complete_for function auto_complete_on_select(element, selectedElement) {   var entityParts ='::');   var entityType = entityParts[0];   var entityId = entityParts[1];   document.getElementById(entityType).value = entityId; }

So all of this renders my form as:   <label for="child_parent_id">parent</label><br/>

  <input id="parent_name" name="parent[name]" size="30" type="text" /

<div class="auto_complete" id="parent_name_auto_complete"></ <script type="text/javascript">

//<![CDATA[ var parent_name_auto_completer = new Ajax.Autocompleter('parent_name', 'parent_name_auto_complete', '/children/ auto_complete_for_parent_name', {afterUpdateElement:auto_complete_on_select, parameters:'authenticity_token=' + encodeURIComponent('2789eb52554dda1a74e3363f76966983ab806fce')}) //]]> </script>     <input id="child_parent_id" name="child[parent_id]" type="hidden" /


What's the problem? Well when I submit the form, the validation runs, and I get an error...but the fieldWithErrors tag doesn't get wrapped on the parent_name field.

I'm sure I'm doing something 'wrong' but I'm not clear on what. I've got a feeling that having the field without a model directly attached is not rails approved, but I'm confused about that since the collection_select is such a common thing...this would seem to be just a direct extension to that.

I've tried adding to the errors hash in the model (this doesn't work):     def after_validation       parent_error = errors.on(:parent)       if parent_error         errors.add(:parent_name, parent_error)         errors.add('parent_name', parent_error)         errors.add('parent[name]', parent_error)       end     end

Now I'm certain I can hack my way out with either: - add some sort of accessor or something like that to child.rb - re-do my modifications to the autocomplete stuff to end up with an id like child[parent_name] - change my view to this..but this is definitely an ugly non- extensible hack:   <% if @child.errors.on(:parent) -%><div class='fieldWithErrors'><% end %>   <%= text_field_with_auto_complete :parent, :name,                                     { :value => @parent_name },                                     { :skip_style => true,                                       :after_update_element => 'auto_complete_on_select' } %>   <% if @child.errors.on(:parent) -%></div><% end %>

Thanks in advance, Dale

So this is the rails way...go watch: #102 Auto-Complete Association - RailsCasts Thanks Ryan.