views:

1854

answers:

1

I have the following select list.

<%= select_tag(:editlevel,options_from_collection_for_select(Level.all, :id, :name)) %>
when the user selects an option from the above list, the list below should be populated with values from database based on the above selection.

<%= select_tag(:lques,{},{ :size =>10, :style => "width:200px"}) %>

i think i should make use of a remote_function with the onchange event. but i have no idea on how to use it. and populate the latter list the values from the database. someone help me please.

thanks in advance.

+1  A: 

a simple way that i solved this was

<select id="editlevel" name="editlevel" onchange="
   <%= remote_function(
           :update => 'lques', 
           :url => {:action => :lques}, 
           :with => "'level=' + $('editlevel').value %>
">

</select>

<select id="lques" name="lques">
</select>

then your lques action can just get what lques records you need and do a options_for_select and it should just put what comes back from the server as $('lques').innerHTML

this is air code, not tested

ErsatzRyan
As a note, try and not use onchange and make the page use unobtrusive javascript when you must use javascript.There are plenty of examples on line where you could do something like: input.onchange = function() { populateFields(); }It makes the HTML cleaner to read when you get back to it and will give you the opportunity to DRY up your views should you need the same functionality again.
Omar Qureshi