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! -- Posted via http://www.ruby-forum.com/.
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 ''<option value="1"> choice 1-1 </option><option value="2"> choice 1-2 </option>'' else render_text ''<option value="1"> choice 2-1 </option><option value="2"> choice 2-2 </option>'' end end View: <html> <head> <%= javascript_include_tag "prototype" %> <%= javascript_include_tag "scriptaculous" %> </head> <body> <div>Select here:</div> <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> </select> </div> <div>Result here</div> <div> <select id="s2"> </select> </body> </html> Hope this helps. Regards, Nicolas On 6/1/06, Alain Pilon <mantat@videotron.ca> wrote:> > 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! > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060601/381b490c/attachment-0001.html
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 -- Posted via http://www.ruby-forum.com/.
Nicolas Buet
2006-Jun-02 09:45 UTC
[Rails] Re: how to: update a select from another select
the :with => "''value='' + escape(value)" is precious! Never quite understood how it works so well, but it does a great job. On 6/2/06, Alain Pilon <mantat@videotron.ca> wrote:> > 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 > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060602/9e9d720a/attachment-0001.html
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 On 6/2/06, Nicolas Buet <nicolas.buet@gmail.com> wrote:> > the :with => "''value='' + escape(value)" is precious! Never quite > understood how it works so well, but it does a great job. > > > On 6/2/06, Alain Pilon <mantat@videotron.ca> wrote: > > > > 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 > > > > -- > > Posted via http://www.ruby-forum.com/. > > _______________________________________________ > > Rails mailing list > > Rails@lists.rubyonrails.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060602/3958625a/attachment.html