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

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!

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

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.