# Ajax autocomplete for two fields

**URL:** <https://www.ruby-forum.com/t/ajax-autocomplete-for-two-fields/89713>\
**Category:** Rails\
**Created:** [March 22, 2007, 8:16am UTC](https://www.ruby-forum.com/t/ajax-autocomplete-for-two-fields/89713 "2007-03-22T08:16:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![moo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c4cdca/32.png) [@moo](https://www.ruby-forum.com/u/moo)\
**Post date:** [March 22, 2007, 8:16am UTC](https://www.ruby-forum.com/t/ajax-autocomplete-for-two-fields/89713/1 "2007-03-22T08:16:43Z")

</div>

in a form if i have two autocomplete fields in which one depend on  
other…

like for ex…if we select country name…all the states should  
come …both are autocomplete…

please help me in this regards…

moyeen.

---

<div class="post-metadata">

**Author:** ![moo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c4cdca/32.png) [@moo](https://www.ruby-forum.com/u/moo)\
**Post date:** [March 22, 2007, 1:11pm UTC](https://www.ruby-forum.com/t/ajax-autocomplete-for-two-fields/89713/2 "2007-03-22T13:11:44Z")

</div>

this needs to be DRYed  
my app is a “Getting Things Done” type.  
so in my “inbox” view I can add it to a project and add context to it  
using the ‘take\_action’ method.

in my ‘inbox’ controller…  
def take\_action  
@item = Item.find(params[:id])  
end

# add project and context

def associate\_project\_context  
@item = Item.find(params[:id])  
@item.project =  
Project.find\_or\_create\_by\_name(params[:item][“project\_id”])  
@item.context =  
Context.find\_or\_create\_by\_name(params[:item][“context\_id”])  
if @item.update\_attributes(params[:item])  
flash[:notice] = ‘Project was successfully associated to item.’  
redirect\_to :action =\> ‘list’  
else  
render :action =\> ‘new’  
end  
end

def auto\_complete\_for\_item\_project\_id  
auto\_complete\_responder\_for\_projects params[:item][:project\_id]  
end

def auto\_complete\_for\_item\_context\_id  
auto\_complete\_responder\_for\_contexts params[:item][:context\_id]  
end

private  
def auto\_complete\_responder\_for\_projects(value)  
@projects = Project.find(:all,  
:conditions =\> [ ‘LOWER(name) LIKE ?’,  
‘%’ + value.downcase + ‘%’ ],  
:order =\> ‘name ASC’)  
render :partial =\> ‘projects’  
end

## def auto\_complete\_responder\_for\_contexts(value) @contexts = Context.find(:all, :conditions =\> [‘LOWER(name) LIKE ?’, ‘%’ + value.downcase + ‘%’], :order =\> ‘name ASC’) render :partial =\> ‘contexts’ end

view with form for the two auto complete boxes

\<%= @item.name %\>

add a project and context \<%= start\_form\_tag :action =\> 'associate\_project\_context', :id =\> @item %\> project: \<%= text\_field\_with\_auto\_complete :item, :project\_id, {} %\> context: \<%= text\_field\_with\_auto\_complete :item, :context\_id, {} %\>

```
<%= submit_tag 'add' %>

```

\<%= end\_form\_tag %\>

\_context.rthml

\<% for context in @contexts do -%\> - \<%=h context.name %\>
\<% end -%\>

\_projects.rhtml

\<% for project in @projects do -%\> - \<%=h project.name %\>
\<% end -%\>
--------------------------------------------------------------------------

maybe someone could suggest on DRYing it up?

---

<div class="post-metadata">

**Author:** ![moo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c4cdca/32.png) [@moo](https://www.ruby-forum.com/u/moo)\
**Post date:** [March 22, 2007, 1:22pm UTC](https://www.ruby-forum.com/t/ajax-autocomplete-for-two-fields/89713/3 "2007-03-22T13:22:46Z")

</div>

On Mar 22, 2007, at 8:15 AM, moo wrote:

> in a form if i have two autocomplete fields in which one depend on  
> other…
> 
> like for ex…if we select country name…all the states should  
> come …both are autocomplete…

The query for states needs a country id or it is enough to receive  
the country name?

– fxn
