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

**URL:** <https://www.ruby-forum.com/t/adding-errors-to-an-autocomplete-field-and-ensuring-it-is-wrapped-with-fieldwitherrors/135064>\
**Category:** Rails\
**Created:** [April 17, 2008, 3:38pm UTC](https://www.ruby-forum.com/t/adding-errors-to-an-autocomplete-field-and-ensuring-it-is-wrapped-with-fieldwitherrors/135064 "2008-04-17T15:38:51Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![dale](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ecd19e/32.png) [@dale](https://www.ruby-forum.com/u/dale)\
**Post date:** [April 17, 2008, 3:38pm UTC](https://www.ruby-forum.com/t/adding-errors-to-an-autocomplete-field-and-ensuring-it-is-wrapped-with-fieldwitherrors/135064/1 "2008-04-17T15:38:51Z")

</div>

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:  
parent  
  
\<%= 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  
#  
[http://ricardo.pacheco.name/blog/articles/2006/09/17/using-autocomplete-with-belongs\_to-fields-part-ii](http://ricardo.pacheco.name/blog/articles/2006/09/17/using-autocomplete-with-belongs_to-fields-part-ii)  
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 = selectedElement.id.split(‘::’);  
var entityType = entityParts[0];  
var entityId = entityParts[1];  
document.getElementById(entityType).value = entityId;  
}

So all of this renders my form as:  
parent

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

> \</

div\>  
\<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) -%\>
\<%  
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,  
Dale

---

<div class="post-metadata">

**Author:** ![dale](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ecd19e/32.png) [@dale](https://www.ruby-forum.com/u/dale)\
**Post date:** [April 30, 2008, 5:00pm UTC](https://www.ruby-forum.com/t/adding-errors-to-an-autocomplete-field-and-ensuring-it-is-wrapped-with-fieldwitherrors/135064/2 "2008-04-30T17:00:42Z")

</div>

So this is the rails way…go watch: [#102 Auto-Complete Association - RailsCasts](http://railscasts.com/episodes/102)  
Thanks Ryan.
