Newbie question - ajax drop downs

How do you do ajax drop downs? I was looking over at
http://www.roryhansen.ca/?p=9

but couldn’t get it to work properly. I decided to try and use
form_remote, but it would submit the form instead of preformaing an ajax
call.

Since I need 2 form elemnts to preform an ajax calls, will they need to
be in seperate forms?

controller
test_update
@counties = County.find(:all,
:conditions => [“state = ?”, params[:state_id]])

@html = “<select id=’county_id’ name=’county_id’>”
@html += “<option value='’>select county</option>”
@counties.each do |cty|
  @html += “<option value=’#{cty.id}’>#{cty.name}</option>”
end
@html += “</select>”

end

index
… code to get state array, generate other stuff
end

index.rhtml

<form_remote_tag :html => {
url_for{:controller => “locator”, action => test_update) } %>
State

Select State <% @states.each do |state| %> ”> <%= state.name %> <% end %> <%= end_form_tag %>

County

Select County

City
… the next form

well its not as straight forward as I would have liked, but I came up
with a solution to this problem.

Basically I created a couple partials that create a single dropdown with
any option values inside. Then in the index I write stubs with the same
div ID as the dropdowns have

I have a javascript function that submits the value of the previous drop
down via Ajax, which queries the database for the records that should be
in the next DD.

Then using a rjs file, I remove the stateDD div, and call the partial to
add it back in the correct place.

There might be better ways of doing this, and I would love to hear them,
but for now this is working.

  • NG