0

I have a rails 4 app where I have a script like:

<script type='text/javascript'>
   $(function (){
      $(document).on("change",".selectProduct", function (){
         var selectValue = $(this).val();
         alert("<%= render 'product_form/test', f:builder %>");
      });
   });
</script>

Basically, what I want to do is have the selectValue (which is the value of a select), be rendered in the ruby code. So something like:

var selectValue = $(this).val();
alert("<%= render 'product_form/#{selectValue}', f:builder %>");
2
  • You can't make Ruby works directly with Ruby: Ruby is executed on the server-side, whereas Javascript is executed on the client-side. You need to send the value selected with Javascript, the server responds (in your case, with HTML), the client gets the response and handle (in your case, display) the response. Commented Jun 18, 2014 at 18:28
  • You can generate .js template in Rails view and interpolate it in Javascript. Commented Jun 18, 2014 at 19:12

1 Answer 1

1

Not exactly what you asked for (you can't call Ruby from JS except for sending AJAX request), but this hybrid approach is close:

... <div>Product: {{product}}</div> ...

  • Render it to a string and pass to JS.

  • alert(Mustache.render(your_template, {product: selectValue}))

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.