# Autocomplete with \_id (if query exists), else create new

**URL:** <https://www.ruby-forum.com/t/autocomplete-with-id-if-query-exists-else-create-new/115438>\
**Category:** Rails\
**Created:** [September 25, 2007, 9:40am UTC](https://www.ruby-forum.com/t/autocomplete-with-id-if-query-exists-else-create-new/115438 "2007-09-25T09:40:53Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![gustav](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/919ad9/32.png) [@gustav](https://www.ruby-forum.com/u/gustav)\
**Post date:** [September 25, 2007, 9:40am UTC](https://www.ruby-forum.com/t/autocomplete-with-id-if-query-exists-else-create-new/115438/1 "2007-09-25T09:40:53Z")

</div>

Hi guys, I’m trying to wrap my head around the rails  
autocompletion-functionality. I’m building a simple form for creating  
tasks, in which I would like to substitute a dropdown-list consisting of  
a collection of projects for an autocomplete-field.

The completed project should be added with the task.project\_id. Also, if  
task is submitted with a project that isn’t in the database, I would  
like a new project to be created and use the newly created project\_id  
for the task.

This is where I’m at now. The form in my view looks like this:

\<% form\_remote\_tag :url =\> { :action =\> ‘newtask’ }, :update =\>  
‘task\_list’ do %\>

Task: \<%= text\_field 'task', 'description' %\> Project: \<%= text\_field\_with\_auto\_complete 'task', 'project' %\> \<%= submit\_tag "New task!" %\>

\<% end %\>

and my controller looks like this:

@newtask = Task.new  
@projects = Project.find(:all)  
@tasks = Task.find(:all)  
@task.project = @params[:project][:name]

I’ve added the include javascript snippet in my layout and the  
“auto\_complete\_for :task, :project” in my . I know I’m missing both the  
\_id-connection and the create a new project. I figure that it should  
probably be solved on a controller-level, but I don’t know where to  
start… Does anyone have any pointers on how I should progress from  
here?

Any help is much appreciated!

---

<div class="post-metadata">

**Author:** ![gustav](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/919ad9/32.png) [@gustav](https://www.ruby-forum.com/u/gustav)\
**Post date:** [September 25, 2007, 12:05pm UTC](https://www.ruby-forum.com/t/autocomplete-with-id-if-query-exists-else-create-new/115438/2 "2007-09-25T12:05:47Z")

</div>

On Sep 25, 2007, at 9:40 AM, Gus Vs wrote:

> for the task.  
> Have a look at model\_auto\_completer:

[http://agilewebdevelopment.com/plugins/model\_auto\_completer](http://agilewebdevelopment.com/plugins/model_auto_completer)

It maintains the project\_id for you, and has an  
option :allow\_free\_text that allows the user to enter free text, in  
which case the project\_id is set to the empty string. In the  
application a blank project\_id means a project to be created.

– fxn

Disclaimer: I am the author.
