Ajax question...need some help

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 ?
:frowning:

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 ?
:frowning:

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/
^ assert_xpath
http://tinyurl.com/yrc77g <-- assert_latest Model

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

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 ?
:frowning:


Sincerely,

William P.

http://www.billpratt.net
[email protected]

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 :slight_smile: thanks :slight_smile:

@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 :slight_smile: