Forum: Ruby on Rails how to: update a select from another select

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Ab4c5cd5d9cc028fcba7a5eec8e1bf30?d=identicon&s=25 Alain Pilon (alain)
on 2006-06-01 12:15
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

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

C29d9db03853958c4de5ee6b06edd39a?d=identicon&s=25 Nicolas Buet (Guest)
on 2006-06-01 15:00
(Received via mailing list)
I just made a quick and dirty example for you:

def select


def set_value
    if @params[:value].to_i == 1
        render_text '<option value="1"> choice 1-1 </option><option
value="2"> choice 1-2 </option>'
        render_text '<option value="1"> choice 2-1 </option><option
value="2"> choice 2-2 </option>'


<%= javascript_include_tag "prototype" %>
<%= javascript_include_tag "scriptaculous" %>
<div>Select here:</div>
<select id="s1" onchange="<%= remote_function(:update => "s2", :url =>
{:action => "set_value"}, :with => "'value=' + escape(value)" ) %>" >
    <option value="1"> val 1 </option>
    <option value="2"> val 2 </option>

<div>Result here</div>
<select id="s2">


Hope this helps.


Ab4c5cd5d9cc028fcba7a5eec8e1bf30?d=identicon&s=25 Alain Pilon (alain)
on 2006-06-02 00:07
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 ;-)

Thanks again
C29d9db03853958c4de5ee6b06edd39a?d=identicon&s=25 Nicolas Buet (Guest)
on 2006-06-02 11:47
(Received via mailing list)
the :with => "'value=' + escape(value)" is precious! Never quite
how it works so well, but it does a great job.
9d1f5d2d9de70bd9a934f557dc95a406?d=identicon&s=25 Daniel ----- (liquid)
on 2006-06-02 14:07
(Received via mailing list)
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.
This topic is locked and can not be replied to.