Forum: Ruby on Rails ajax autocomplete for two fields..............

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
moo (Guest)
on 2007-03-22 09:16
(Received via mailing list)
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.
John I. (Guest)
on 2007-03-22 14:11
(Received via mailing list)
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
<p><%= @item.name %></p>
<fieldset>
<legend>add a project and context</legend>
<%= 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 %>
</fieldset>

_context.rthml
<ul class="projects">
<% for context in @contexts do -%>
  <li><%=h context.name %></li>
<% end -%>
</ul>

_projects.rhtml
<ul class="projects">
<% for project in @projects do -%>
  <li><%=h project.name %></li>
<% end -%>
</ul>
--------------------------------------------------------------------------


maybe someone could suggest on DRYing it up?
Xavier N. (Guest)
on 2007-03-22 14:22
(Received via mailing list)
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
This topic is locked and can not be replied to.