Newbie question - ajax drop downs

How do you do ajax drop downs? I was looking over at

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

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

@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=’#{}’>#{}</option>”
@html += “</select>”


… code to get state array, generate other stuff


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

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


Select County

… 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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs