问题
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:
From
"<%= escape_javascript(render :partial =>'my_partial', :locals => { id:id} ) %>"
To
"<%= escape_javascript(render :partial =>'my_partial', :locals => { id:"+id+"} ) %>"
回答2:
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);
来源:https://stackoverflow.com/questions/38794605/how-to-use-variable-in-javascript-escape-javascript-rails