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:
belongs_to :parent
validates_presence_of :parent
validates_presence_of :name

has_many :children


basic scaffold style controller with an method to support ajax:

indexed_auto_complete_for :child, :parent, :name


<%= 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,


    render :inline => "<%= indexed_auto_complete_result @items,

‘#{parent}_#{object}_id’, ‘#{method}’, ‘id’ %>"

modified application.js
// this methods are to allow autocomplete on :belongs_to a bit easier,
// - 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:

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


<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)

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) -%>
    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) -%>
    <% end %>

Thanks in advance,

So this is the rails way…go watch:
Thanks Ryan.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs