0

I need to pass a variable js to rails, as a parameter, but it is not working. Here is my code:

#function js
function load_custom_fields(){
  var id = $("MY_SELECT").val();
  $(".productHere").html("<%= escape_javascript(render :partial =>'my_partial', :locals => { id:id} ) %>");

}
1
  • Will this work? $(".productHere").html("<%= escape_javascript(render :partial =>'my_partial', :locals => { id: " + id + "} ) %>"); Commented Aug 5, 2016 at 17:44

2 Answers 2

1

From

"<%= escape_javascript(render :partial =>'my_partial', :locals => { id:id} ) %>"

To

"<%= escape_javascript(render :partial =>'my_partial', :locals => { id:"+id+"} ) %>"
Sign up to request clarification or add additional context in comments.

1 Comment

this came as a string, answer = + id +
1

A simple solution is to add a placeholder inside the escape_javascript and then replace it with your JavaScript variable, for example:

var id = $("MY_SELECT").val();
$(".productHere").html("<%= escape_javascript(render :partial =>'my_partial', :locals => { id: '_ID_'} ) %>".replace("_ID_", id);

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.