# Ajax question...need some help

**URL:** <https://www.ruby-forum.com/t/ajax-question-need-some-help/112361>\
**Category:** Rails\
**Created:** [August 17, 2007, 10:47pm UTC](https://www.ruby-forum.com/t/ajax-question-need-some-help/112361 "2007-08-17T22:47:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/919ad9/32.png) [@mix](https://www.ruby-forum.com/u/mix)\
**Post date:** [August 17, 2007, 10:47pm UTC](https://www.ruby-forum.com/t/ajax-question-need-some-help/112361/1 "2007-08-17T22:47:07Z")

</div>

hi everybody, how can i do something like this?  
i have a select with something to choose, when you choose one of them  
the select must be reload with new records, until the records are  
finished (2 or 3 levels)…i think that i can do this using ajax, but  
how?  
or another way is to have a first select, when is chose something from  
that another select appear with the new records to select… any idea ?  
☹

---

<div class="post-metadata">

**Author:** ![mix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/919ad9/32.png) [@mix](https://www.ruby-forum.com/u/mix)\
**Post date:** [August 17, 2007, 11:36pm UTC](https://www.ruby-forum.com/t/ajax-question-need-some-help/112361/2 "2007-08-17T23:36:48Z")

</div>

Mix M. wrote:

> hi everybody, how can i do something like this?  
> i have a select with something to choose, when you choose one of them  
> the select must be reload with new records, until the records are  
> finished (2 or 3 levels)…i think that i can do this using ajax, but  
> how?  
> or another way is to have a first select, when is chose something from  
> that another select appear with the new records to select… any idea ?  
> ☹

Let’s take the model of countries, states, and cities. Unless your  
users are creating countries, the list of all cities never changes,  
even if it is very large.

So let’s preload that entire data object by building a Ruby Hash of  
countries, states, and cities, and use to\_json to extrude all of it as  
a JavaScript variable. Then use javascript\_tag to stick this into the

 of your page. (If you worry about server load, you could just put the list into a .js file, and then only the browsers who haven't seen the list yet will load it.)

Then you use JavaScript to populate the select, and when the user  
picks an item you hook the onchange event, and use more JavaScript to  
repopulate.

This is a fairly well-established pattern, so you should be able to  
Google for it!

–  
Phlip  
[http://www.oreilly.com/catalog/9780596510657/](http://www.oreilly.com/catalog/9780596510657/)  
^ assert\_xpath  
[O'Reilly Media - Technology and Business Training](http://tinyurl.com/yrc77g) ← assert\_latest Model

---

<div class="post-metadata">

**Author:** ![mix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/919ad9/32.png) [@mix](https://www.ruby-forum.com/u/mix)\
**Post date:** [August 18, 2007, 1:24am UTC](https://www.ruby-forum.com/t/ajax-question-need-some-help/112361/3 "2007-08-18T01:24:58Z")

</div>

I would use an observer like this:

\<%= select\_tag :foo, [1,2,3,4], 1) %\>  
\<%= observe\_field :foo, :url =\> {:action =\> :bar}, :with =\> ‘foo’ -%\>

Make sure you include the :with =\> ‘foo’ as that will become params[foo]  
and will be equal to the selected value in the select

see:  
[http://api.rubyonrails.org/classes/ActionView/Helpers/PrototypeHelper.html#M000535](http://api.rubyonrails.org/classes/ActionView/Helpers/PrototypeHelper.html#M000535)

Mix M. wrote:

> hi everybody, how can i do something like this?  
> i have a select with something to choose, when you choose one of them  
> the select must be reload with new records, until the records are  
> finished (2 or 3 levels)…i think that i can do this using ajax, but  
> how?  
> or another way is to have a first select, when is chose something from  
> that another select appear with the new records to select… any idea ?  
> ☹

–  
Sincerely,

William P.

[http://www.billpratt.net](http://www.billpratt.net)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![mix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/919ad9/32.png) [@mix](https://www.ruby-forum.com/u/mix)\
**Post date:** [August 28, 2007, 2:13pm UTC](https://www.ruby-forum.com/t/ajax-question-need-some-help/112361/4 "2007-08-28T14:13:54Z")

</div>

William P. wrote:

> I would use an observer like this:
> 
> \<%= select\_tag :foo, [1,2,3,4], 1) %\>  
> \<%= observe\_field :foo, :url =\> {:action =\> :bar}, :with =\> ‘foo’ -%\>

I didn’t know about observed… i’ve used it 🙂 thanks 🙂

@Phlip

The problem was that i’ve a list of dynamic objects, which can changes  
every time, and with a non definite level of depth… anyway i’ve done  
this using the observer, and it works great 🙂
