9

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?

99miles
  • 10,942
  • 18
  • 78
  • 123
  • 2
    Um, well, for one thing, and this is really just to get started, that line of erb isn't outputting anything because it should be '<%=' instead of '<%'. Beyond that, I think you're going to have to post the contents of that partial, be more specific about the behavior you're looking for. – tfwright Feb 25 '10 at 04:05

2 Answers2

9

I think this is what you are looking for. I am rendering the partial within a DIV with "colors" ID, the partial is rendered only if the selected radio button's value is red:

<script language="javascript" type="text/javascript">
jQuery(function($){
  $('#radio-buttons input[type=radio]').click(function() {
    if ($(this).attr("value") === "red") {
      $("#colors").html('#{escape_javascript(render :partial => "colors")}');
    }
  });
});
</script>
miligraf
  • 1,042
  • 9
  • 22
  • 2
    I tried it. This simply print #{escape_javascript(render :partial => "")} on page as text. – Bongs Mar 01 '13 at 08:21
  • 3
    @Bongs This is assumed to go into a .erb file so that the ruby, #{escape_javascript(render :partial => "")}, will be replaced appropriately instead of being interpreted as a string literal. – jeconner Mar 04 '13 at 17:27
  • 3
    A few major things. Even in a .erb file you would need to wrap your string in <%= %>, and make sure to use double quotes. Ruby does not do string interpolation with single quotes. This will also only render once. The question clearly is looking to render upon a javascript action. downvoted – TheJKFever Jul 29 '15 at 23:42
  • @TheJKFever does that mean if I wanted something to only render once, I could use this script to render a partial, say, on a button-click? `.html(<%= "#{escape_javascript(render :partial => "colors")}" %>);` – Danodemo Feb 04 '20 at 18:55
7

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.

Mike Trpcic
  • 25,305
  • 8
  • 78
  • 114