# Dynamic SelectBoxes/DropDownLists

**URL:** <https://www.ruby-forum.com/t/dynamic-selectboxes-dropdownlists/100837>\
**Category:** Rails\
**Created:** [December 12, 2005, 11:35am UTC](https://www.ruby-forum.com/t/dynamic-selectboxes-dropdownlists/100837 "2005-12-12T11:35:10Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![milos\_kelemen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a6a055/32.png) [@milos\_kelemen](https://www.ruby-forum.com/u/milos_kelemen)\
**Post date:** [December 12, 2005, 11:35am UTC](https://www.ruby-forum.com/t/dynamic-selectboxes-dropdownlists/100837/1 "2005-12-12T11:35:10Z")

</div>

Hi,

I’d like to implement in my project dynamic dropdown lists.  
Example:  
user will select one option from the parent dropdown list, lets say  
it’ll be COUNTRY and in the child dropdown will be generated all CITIES  
tied with this selected country.

I could then have relevant info in my form. Country and one of its city.

I know, this is not much abour RoR, however it has something to do w/  
Ajax. I really like autocomplete feature in RoR and I’m sure this  
dynamic dropdowns should be done easy in Ajax too.

any clues…?

thanks and have a great day

-\> keli (milos kelemen)

---

<div class="post-metadata">

**Author:** ![milos\_kelemen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a6a055/32.png) [@milos\_kelemen](https://www.ruby-forum.com/u/milos_kelemen)\
**Post date:** [December 12, 2005, 12:27pm UTC](https://www.ruby-forum.com/t/dynamic-selectboxes-dropdownlists/100837/2 "2005-12-12T12:27:42Z")

</div>

milos kelemen wrote:\>

> I’d like to implement in my project dynamic dropdown lists.  
> Example:  
> user will select one option from the parent dropdown list, lets say  
> it’ll be COUNTRY and in the child dropdown will be generated all CITIES  
> tied with this selected country.

You could use an observe\_field to monitor the choice in the first select  
box, as in this example:  
[http://www.roryhansen.ca/?p=9](http://www.roryhansen.ca/?p=9)

This is something I’m currently experimenting with myself - will let you  
know how it goes.

---

<div class="post-metadata">

**Author:** ![milos\_kelemen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a6a055/32.png) [@milos\_kelemen](https://www.ruby-forum.com/u/milos_kelemen)\
**Post date:** [December 12, 2005, 1:23pm UTC](https://www.ruby-forum.com/t/dynamic-selectboxes-dropdownlists/100837/3 "2005-12-12T13:23:19Z")

</div>

Nick Ce wrote:

> box, as in this example:  
> [http://www.roryhansen.ca/?p=9](http://www.roryhansen.ca/?p=9)
> 
> This is something I’m currently experimenting with myself - will let you  
> know how it goes.

tnak you nick,

I’ll let you know how it goes,

→ keli

---

<div class="post-metadata">

**Author:** ![milos\_kelemen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a6a055/32.png) [@milos\_kelemen](https://www.ruby-forum.com/u/milos_kelemen)\
**Post date:** [December 12, 2005, 2:44pm UTC](https://www.ruby-forum.com/t/dynamic-selectboxes-dropdownlists/100837/4 "2005-12-12T14:44:45Z")

</div>

Nick Ce wrote:

> box, as in this example:  
> [http://www.roryhansen.ca/?p=9](http://www.roryhansen.ca/?p=9)
> 
> This is something I’m currently experimenting with myself - will let you  
> know how it goes.

Nick,

thanks for your help again…

this script works as it should

---

<div class="post-metadata">

**Author:** ![milos\_kelemen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a6a055/32.png) [@milos\_kelemen](https://www.ruby-forum.com/u/milos_kelemen)\
**Post date:** [December 12, 2005, 3:32pm UTC](https://www.ruby-forum.com/t/dynamic-selectboxes-dropdownlists/100837/5 "2005-12-12T15:32:57Z")

</div>

hi,  
someone asked the exact same question here …see this  
[http://article.gmane.org/gmane.comp.lang.ruby.rails/32790/match=](http://article.gmane.org/gmane.comp.lang.ruby.rails/32790/match=)  
and this  
[http://article.gmane.org/gmane.comp.lang.ruby.rails/33824/match=](http://article.gmane.org/gmane.comp.lang.ruby.rails/33824/match=)  
Vivek

---

<div class="post-metadata">

**Author:** ![milos\_kelemen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a6a055/32.png) [@milos\_kelemen](https://www.ruby-forum.com/u/milos_kelemen)\
**Post date:** [December 12, 2005, 11:52pm UTC](https://www.ruby-forum.com/t/dynamic-selectboxes-dropdownlists/100837/6 "2005-12-12T23:52:20Z")

</div>

milos kelemen wrote:

> Nick,
> 
> thanks for your help again…
> 
> this script works as it should

No problem - glad yours works ok - still having problems with mine ☹  
I decided to go down the partials route as used in the posts that Vivek  
linked to, here  
[http://article.gmane.org/gmane.comp.lang.ruby.rails/33824/match=](http://article.gmane.org/gmane.comp.lang.ruby.rails/33824/match=) and  
here [http://article.gmane.org/gmane.comp.lang.ruby.rails/33824/match=](http://article.gmane.org/gmane.comp.lang.ruby.rails/33824/match=)

At present I have this: [http://www.rafb.net/paste/results/YxkO1V47.html](http://www.rafb.net/paste/results/YxkO1V47.html)

Unfortunately, when I make a choice on the first select, no content  
appears within the person\_list div - can anyone see anything wrong at  
all?

---

<div class="post-metadata">

**Author:** ![milos\_kelemen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a6a055/32.png) [@milos\_kelemen](https://www.ruby-forum.com/u/milos_kelemen)\
**Post date:** [December 13, 2005, 1:23am UTC](https://www.ruby-forum.com/t/dynamic-selectboxes-dropdownlists/100837/7 "2005-12-13T01:23:34Z")

</div>

Are you using Firefox? If so, make sure that the ID of the select box is  
actually job\_company\_id. I’m not familiar with the output of  
collection\_select, but it may output only the NAME attribute and not the  
ID  
unless you explicitly tell it to.

---

<div class="post-metadata">

**Author:** ![milos\_kelemen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a6a055/32.png) [@milos\_kelemen](https://www.ruby-forum.com/u/milos_kelemen)\
**Post date:** [December 13, 2005, 6:42am UTC](https://www.ruby-forum.com/t/dynamic-selectboxes-dropdownlists/100837/8 "2005-12-13T06:42:18Z")

</div>

In the post which I linked to…I didnt understand the use of :object …I  
know of :collection which calls the template repeatedly…whats with this  
:object ? does anyone here have an idea?

I saw rory’s solution… easier to follow…Thanks for the tip.  
Vivek

---

<div class="post-metadata">

**Author:** ![milos\_kelemen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a6a055/32.png) [@milos\_kelemen](https://www.ruby-forum.com/u/milos_kelemen)\
**Post date:** [December 13, 2005, 10:16am UTC](https://www.ruby-forum.com/t/dynamic-selectboxes-dropdownlists/100837/9 "2005-12-13T10:16:37Z")

</div>

Rory Hansen wrote:

> Are you using Firefox? If so, make sure that the ID of the select box is  
> actually job\_company\_id. I’m not familiar with the output of  
> collection\_select, but it may output only the NAME attribute and not the  
> ID unless you explicitly tell it to.

Thanks for the reply Rory. I’m actually using Camino, and the  
collection\_select generates this:

Client  
company A company B

which looks ok to me.

Vivek wrote:

> In the post which I linked to…I didnt understand the use of :object …I  
> know of :collection which calls the template repeatedly…whats with this  
> :object ? does anyone here have an idea?

From the Agile Rails book: “The :object parameter identifies an object  
to be passed into the partial. This object will be available within the  
template via a local variable with the same name as the template.” Not  
sure I understand this fully - does my error lie with the naming of the  
object variable perhaps? Still can’t get it to work as expected. The DIV  
I’m trying to update is nested within a form; will that make any  
difference? [http://www.rafb.net/paste/results/YxkO1V47.html](http://www.rafb.net/paste/results/YxkO1V47.html)

---

<div class="post-metadata">

**Author:** ![milos\_kelemen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a6a055/32.png) [@milos\_kelemen](https://www.ruby-forum.com/u/milos_kelemen)\
**Post date:** [December 13, 2005, 10:34am UTC](https://www.ruby-forum.com/t/dynamic-selectboxes-dropdownlists/100837/10 "2005-12-13T10:34:09Z")

</div>

Nick C wrote:

> [http://www.rafb.net/paste/results/YxkO1V47.html](http://www.rafb.net/paste/results/YxkO1V47.html)

Solved it - it was a syntax error - one measly single quote missing from  
:with =\> “‘job\_company\_id=’+value”

The partial method in conjunction with collection\_select certainly seems  
a tidy way of doing dynamic drop downs.

Thanks to all those who chipped in.
