Populating a select box on changing another selectbox elemnt

Hello frnds,
I hav 2 select boxes.
First select box is populated with all the “distinct states”.On
selecting a state I want the 2nd selct box to be populated with its
corresponding “districts”.
On selecting the district,I want some data to be displayed for that
corressponding district.
I am able to display the data on selecting the “district”.(using RJS
template and partial)
But the 2nd select box is always populated with all the districts.It is
not populated accoring to the selection of first selectbox.
Can you help me in solving this problem?

things that i tried

Readings controller
def index
@states= Station.find(:all, :select=>‘state’, :order =>“state”).map{
|t| t.state }.uniq
if (params[:state] )
@districts = Station.find(district,
@districts = Station.find(:all, :select=>‘district’).map{ |t|
t.district }.uniq


<% form_for :selected_district do |f| %>

<%=observe_field :selected_district_state, :frequency => 0.5, :update => "district_div",:url =>{:action => 'index' },:with => "'state='+encodeURIComponent(value)" -%>
<%= render :partial => "district_list" %>

<%= f.label :State %>
<%= select(:selected_district,:state, @states,{:include_blank=>'All'}) %>


District : <%= select(:selected_district,:district, @districts,{:include_blank=>'All'},{:onchange =>remote_function(:url=>"/readings/show_readings_for_station",:with =>"'district='+document.getElementById('selected_district_district').value")} ) %>

I also tried with another RJS template.But it also din’t work 4 me

Thanks in advance,

I’m still new to Rails so pardon me if I misunderstand your issue, but I
believe the answer is to use

  1. collection_select
  2. :onchange

You’ll probably find your way here :


Le 8 avr. 2010 à 11:00, Veena J. a écrit :

Hai Christophe,
Thanks 4 ur reply.This is certainly good tutorial…
Thanks a lot…


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