# HOW DO I populate options from previous option click

**URL:** <https://www.ruby-forum.com/t/how-do-i-populate-options-from-previous-option-click/50414>\
**Category:** Rails\
**Created:** [December 16, 2005, 2:05am UTC](https://www.ruby-forum.com/t/how-do-i-populate-options-from-previous-option-click/50414 "2005-12-16T02:05:28Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![soderic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8baadc/32.png) [@soderic](https://www.ruby-forum.com/u/soderic)\
**Post date:** [December 16, 2005, 2:05am UTC](https://www.ruby-forum.com/t/how-do-i-populate-options-from-previous-option-click/50414/1 "2005-12-16T02:05:28Z")

</div>

Hi,  
I have a form with three drop down option lists. Currently each of these  
contain full listings as instantiated in the controller on entering  
the edit action. What I would like to do is filter the second list based  
on what the user selected on the first and the third filtered to reflect  
the second.  
For example I have major\_role, role and person\_in\_role = role is a  
subset of major\_role and person\_in\_role is one qualified to act in role  
Kind Regards,  
Eric

---

<div class="post-metadata">

**Author:** ![soderic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8baadc/32.png) [@soderic](https://www.ruby-forum.com/u/soderic)\
**Post date:** [December 16, 2005, 7:24am UTC](https://www.ruby-forum.com/t/how-do-i-populate-options-from-previous-option-click/50414/2 "2005-12-16T07:24:29Z")

</div>

Eric:

I figure that even if my answer is semi useless, it is still better  
than no answer. I hope you agree. And with that out the way, here  
is the help I can offer.

It seems to me that there are three ways to achieve the effect you want.

1. A round trip to the server. That would be easy, so I doubt that  
is what you want.

2. Some hideously complex javascript stuff that I still have  
nightmares about since I last had to do something like this. (I  
believe debugging javascript has improved since then, firefox is your  
friend).

3. Some wonderful ajax stuff. Sadly, this is where I become  
completely useless to you since I have been focusing on other aspects  
of rails thus far. From what I have read and seen, ajax is going to  
be nicer to you than doing javascript and nicer to the customers than  
round-tripping.

bruce

---

<div class="post-metadata">

**Author:** ![soderic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8baadc/32.png) [@soderic](https://www.ruby-forum.com/u/soderic)\
**Post date:** [December 16, 2005, 2:02pm UTC](https://www.ruby-forum.com/t/how-do-i-populate-options-from-previous-option-click/50414/3 "2005-12-16T14:02:21Z")

</div>

Change this according to your own needs:

1. View:

\<%= select\_tag(‘search[category]’, SELECT OPTIONS HERE) %\>

\<%= observe\_field ‘search[category]’, :url =\> {:action =\>  
‘search\_sub\_category\_xhr’}, :update =\> ‘sub\_category\_wrapper’, :with  
=\> “‘category\_id=’ + escape(value)”, :complete =\> visual\_effect  
(:highlight, ‘sub\_category\_wrapper’) %\>

1. Now create a new action “search\_sub\_category\_xhr”

2. View for “search\_sub\_category\_xhr”

\<%= select\_tag ‘search[sub\_category]’,  
options\_from\_collection\_for\_select(@sub\_categories, ‘id’, ‘name’) %\>
