Monday, June 3, 2024
39
rated 0 times [  42] [ 3]  / answers: 1 / hits: 24226  / 15 Years ago, thu, february 25, 2010, 12:00:00

I have a select that gets populated depending on the selection in a different select
To do this, I did what is recommended in Railscast #88 on 'Dynamic Select Menus'.



But now I need to render a partial, passing in the value selected in each of those selects.
I can't figure out how to simply trigger a method call from the :onchange event in the select, so I'm thinking I need to call render :partial from within the javascript handler that detects the selects change event. But I can't figure out how. I tried the following in the javascript method (in the .js.erb file) but it doesn't work as hoped:



function staffSelected() {
$('date_area').show();
<% render :partial => calendar -%>
}


Anyone have a good solution?


More From » ruby-on-rails

 Answers
11

You can't render a partial inside of JavaScript to detect change events. Ruby is server side, and JavaScript is client side. By the time your JavaScript runs, your Ruby has already been parsed into regular ol' HTML.



Your best bet is to do the following:



Use an AJAX control (there are Prototype/scriptaculous ones) to hit your server in the onchange event. Your server can send back RJS to update the other dropdowns, or it can return back standard JSON and you can populate the other dropdowns manually in the onSuccess method of the AJAX call.


[#97492] Monday, February 22, 2010, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
dawnc

Total Points: 612
Total Questions: 94
Total Answers: 98

Location: Sweden
Member since Fri, Apr 16, 2021
3 Years ago
;