# Auto completion for dynamically created input fields

**URL:** <https://www.ruby-forum.com/t/auto-completion-for-dynamically-created-input-fields/144016>\
**Category:** Rails\
**Created:** [July 24, 2008, 5:49pm UTC](https://www.ruby-forum.com/t/auto-completion-for-dynamically-created-input-fields/144016 "2008-07-24T17:49:54Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![klickverbot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/7993a0/32.png) [@klickverbot](https://www.ruby-forum.com/u/klickverbot)\
**Post date:** [July 24, 2008, 5:49pm UTC](https://www.ruby-forum.com/t/auto-completion-for-dynamically-created-input-fields/144016/1 "2008-07-24T17:49:54Z")

</div>

While working on a small application for a friend of mine, I stumbled  
upon a particularly tricky problem. But first a few lines about my  
general setup:

Among other things, my application is managing Employees, each of which  
has\_and\_belongs\_to\_many Skills. My pretty straightforward  
EmployeesController offers the actions “new” and “edit”, which display a  
form where the user can edit the employee record. The user is able to  
assign skills to an employee by using text fields, if a skill does not  
already exist, it is created on the fly when updating the employee  
record.

This part of the application already works, I implemented the solution  
proposed by Ryan B. in his Railscasts #73-75. Thus my Employee model  
has a virtual attribute skill\_attributes; in the setter method  
skill\_attributes=(), Skill.find\_or\_create\_by\_name() is called for each  
passed skill name. My code for this is not quite elegant, but it works  
just fine:

def skill\_attributes=( skill\_attributes )  
self.skills.clear  
for attributes in skill\_attributes  
unless attributes[‘name’].blank?  
skill = Skill.find\_or\_create\_by\_name( attributes[‘name’] )  
self.skills \<\< skill unless self.skills.include?( skill )  
end  
end  
end

In the \_form for the employees I just render the following partial for  
each skill:

- \<% fields\_for "employee[skill\_attributes][]", skill do |skill\_form| %\> \<%= skill\_form.label :name, 'Skill: ' %\>\<%= skill\_form.text\_field :name, :index =\> nil %\> \<%= link\_to\_function 'remove', "this.up( '.skill' ).remove()" %\> \<% end %\>

Using the “remove” link, the user can remove the skill entry (the

  
with the label and the text\_field), new ones can be added by a link in  
the main form:

link\_to\_function ‘Add skill’ do |page|  
page.insert\_html :bottom, :skills, :partial =\> ‘skill’, :object =\>  
Skill.new  
end

As mentioned above, this part is already working fine. However, I also  
want to add an auto completion feature using AJAX, because this was the  
motivation to use text fields in the first place (in a desktop  
application, I might have used a combo box for this). Using a slightly  
modified version of the auto\_complete plugin, this is no problem in  
other parts of the application.

But in this case I am confronted with the problem that I (obviously)  
have to know the (html) id of each text field to create an  
Ajax.Autocompleter for it. But at the moment, the id shared by all text  
fields (“employee\_skill\_attributes\_\_name”), and I have no idea about a  
“clean” way of assigning an unique one to the ones created by the  
client-side-only JavaScript link.

I can’t get rid of this odd feeling that the solution is waiting right  
under my nose, but I am not enough of a JavaScript programmer to find a  
simple way to accomplish this…

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![klickverbot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/7993a0/32.png) [@klickverbot](https://www.ruby-forum.com/u/klickverbot)\
**Post date:** [December 24, 2008, 7:30am UTC](https://www.ruby-forum.com/t/auto-completion-for-dynamically-created-input-fields/144016/2 "2008-12-24T07:30:51Z")

</div>

Hello,

did you ever solve this problem, combining dynamic fields\_for entries  
with the autocomplete helper? I would like to use the same combination  
(using attribute\_fu) and I’m stuck at the same position.

Thanks!

On 24 Jul., 16:49, Klick V. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![klickverbot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/7993a0/32.png) [@klickverbot](https://www.ruby-forum.com/u/klickverbot)\
**Post date:** [December 24, 2008, 12:41pm UTC](https://www.ruby-forum.com/t/auto-completion-for-dynamically-created-input-fields/144016/3 "2008-12-24T12:41:37Z")

</div>

Hello Jens,

IIRC I went with an approach similar to the one described by Jeff Dean:  
[http://zilkey.com/2008/4/5/complex-forms-with-correct-ids](http://zilkey.com/2008/4/5/complex-forms-with-correct-ids).

There also was a discussion about this issue on the rails-core list a  
while ago, maybe you can find something in the archives. As far as I  
remember, it didn’t yield any concrete results though.
