How to: update a select from another select


#1

Hi all,

I woke up at 4AM this morning to find a solution for this and it is
still not working… grrr! lost sleep for nothing!

Anyways, I have 2 select list and I want the content of the second list
to be updated according to what is selected in the first list.

I did this in the past by having my application generate all the
possible content of the second list and make some Javascript to do the
change. But this time, I cant since these list can have more than 100
elements each. So I need the first select to call an action from my
controller that would then return the string to be inserted in the
select.

Anyone has any idea/exemple how I could to this?

Thanks!


#2

I just made a quick and dirty example for you:

Controller:
def select

end

def set_value
if @params[:value].to_i == 1
render_text ’ choice 1-1 choice 1-2 ’
else
render_text ’ choice 2-1 choice 2-2 ’
end
end

View:

<%= javascript_include_tag "prototype" %> <%= javascript_include_tag "scriptaculous" %>
Select here:
{:action => "set_value"}, :with => "'value=' + escape(value)" ) %>" > val 1 val 2
Result here

Hope this helps.

Regards,

Nicolas


#3

Thanks Nicolas,

Reviewing your code and reading a bit more, I finaly managed to make it
work by using an observer on the list. So when the value changes, the
observer call the action and update the other object.

Your code allowed me to learn about the :width option! Never read about
it before :wink:

Thanks again


#4

the :with => “‘value=’ + escape(value)” is precious! Never quite
understood
how it works so well, but it does a great job.


#5

There are issues with doing this in IE due to the lack of support for
the js
innerHTML method.

Ezra has provided a solution but your mileage may vary.

http://www.ruby-forum.com/topic/61899#62762