# Populating a select box on changing another selectbox elemnt

**URL:** <https://www.ruby-forum.com/t/populating-a-select-box-on-changing-another-selectbox-elemnt/186528>\
**Category:** Rails\
**Created:** [April 8, 2010, 11:00am UTC](https://www.ruby-forum.com/t/populating-a-select-box-on-changing-another-selectbox-elemnt/186528 "2010-04-08T11:00:36Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![veena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/51bf81/32.png) [@veena](https://www.ruby-forum.com/u/veena)\
**Post date:** [April 8, 2010, 11:00am UTC](https://www.ruby-forum.com/t/populating-a-select-box-on-changing-another-selectbox-elemnt/186528/1 "2010-04-08T11:00:36Z")

</div>

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

1. 

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

readings/index.html.erb

\<% 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'}) %\> |

\<%end%\>

\_district\_list.html.erb

**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,  
Veena

---

<div class="post-metadata">

**Author:** ![veena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/51bf81/32.png) [@veena](https://www.ruby-forum.com/u/veena)\
**Post date:** [April 8, 2010, 4:13pm UTC](https://www.ruby-forum.com/t/populating-a-select-box-on-changing-another-selectbox-elemnt/186528/2 "2010-04-08T16:13:46Z")

</div>

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 :  
[http://pullmonkey.com/2008/03/30/dynamic-select-boxes-ruby-on-rails/](http://pullmonkey.com/2008/03/30/dynamic-select-boxes-ruby-on-rails/)

Christophe

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

---

<div class="post-metadata">

**Author:** ![veena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/51bf81/32.png) [@veena](https://www.ruby-forum.com/u/veena)\
**Post date:** [April 9, 2010, 11:10am UTC](https://www.ruby-forum.com/t/populating-a-select-box-on-changing-another-selectbox-elemnt/186528/3 "2010-04-09T11:10:57Z")

</div>

> [PullMonkey Blog » Blog Archive » Dynamic Select Boxes – Ruby on Rails](http://pullmonkey.com/2008/03/30/dynamic-select-boxes-ruby-on-rails/)  
> Hai Christophe,  
> Thanks 4 ur reply.This is certainly good tutorial…  
> Thanks a lot…

Veena
