Update second pulldown menu based on first menu selection?

I know I’ve seen this somewhere on this board and just can’t search it
anymore.

I have three relational tables: clients, projects, and tasks. Currently
I’m working on customizing the ‘new/edit’ task _form. So far I have it
listing clients in a pulldown menu so I can assign the new task to a
client. I’d like to have a second pulldown select menu that would list
the projects for the selected client though. Each time I select a
different client in the first pulldown the second pulldown should
automatically list the related projects for that client without having
to reload the page.

Since the second pulldown would get populated from a call to the
client.projects table I’m guessing it has to be AJAX. Anybody seen RoR
examples or tutorials on such tactics? Any help appreciated.

Thanks,
DAN

On 27 Jul 2006, at 08:32, FreelanceWebDesigner.com wrote:

automatically list the related projects for that client without having
to reload the page.

Since the second pulldown would get populated from a call to the
client.projects table I’m guessing it has to be AJAX. Anybody seen RoR
examples or tutorials on such tactics? Any help appreciated.

Quick and dirty (things can certainly be optimized so that the code
is more DRY, but it should give you an idea):

Main view:

	<p><label for="editrecord_customer_id">Customer : </label><%= select

(:editrecord, :customer_id, @customers.collect {|p| [p.customername,
p.customerid.to_i]}, {:prompt => “— Customer list —”},{:class =>
“required validate-number”})%>


<%= render :partial => ‘projectlist’ %>


<%= render :partial => ‘joblist’ %>


<%= render :partial => ‘tasklist’ %>


<%= observe_field(“editrecord_customer_id”, :url => {:action
=> :findprojects})-%>

_projectlist.rhtml

<% if @projects %>
Project: <%= select
(:editrecord, :project_id, [[“— Choose a project —”,“”]]+
@projects.collect {|p| [p.name,p.id.to_i]}, {},{:class => “required
validate-number”})%>
<% else %>
Project: <%= select
(:editrecord, :project_id, [[“— Choose a customer —”,“”]], {},
{:class => “required validate-number”})%>
<% end %>
<%= observe_field(“editrecord_project_id”, :url => {:action
=> :findjobs})-%>

_joblist.rhtml

<% if @jobs %>
Job: <%= select
(:editrecord, :job_id, [[“— Choose a job —”,“”]][email protected] {|
p| [p.name,p.id.to_i]}, {},{:class => “required validate-number”})%>
<% else %>
Job : <%= select
(:editrecord, :job_id, [[“— Choose a project —”,“”]], {},{:class
=> “required validate-number”})%>
<% end %>
<%= observe_field(“editrecord_job_id”, :url => {:action
=> :findtasks})-%>

_tasklist.rhtml

<% if @tasks %>
Task: <%= select
(:editrecord, :task_id, [[“— Choose a task —”,“”]][email protected]
{|p| [p.name,p.id.to_i]}, {},{:class => “required validate-number”})%>
<% else %>
Task : <%= select
(:editrecord, :task_id, [[“— Choose a job —”,“”]], {},{:class =>
“required validate-number”})%>
<% end %>

In the controller :

def findprojects
@phrase = request.raw_post || request.query_string
if !(@phrase == “”)
@projects=
@jobs=nil
@tasks=nil
end
render :update do |page|
page.replace_html :projectff, :partial => “projectlist”
page.replace_html :jobff, :partial => “joblist”
page.replace_html :taskff, :partial => “tasklist”
end
end
def findjobs
@phrase = request.raw_post || request.query_string
if !(@phrase == “”)
@jobs =
@tasks=nil
end
render :update do |page|
page.replace_html :jobff, :partial => “joblist”
page.replace_html :taskff, :partial => “tasklist”
end
end

def findtasks
@phrase = request.raw_post || request.query_string
if !(@phrase == “”)
@tasks =
end
render :update do |page|
page.replace_html :taskff, :partial => “tasklist”
end
end

Hope this helps!

Best regards

Peter De Berdt