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} ) %>");
}
From
"<%= escape_javascript(render :partial =>'my_partial', :locals => { id:id} ) %>"
To
"<%= escape_javascript(render :partial =>'my_partial', :locals => { id:"+id+"} ) %>"
You can use JS-interpolation. For this use backticks and ${var}
:
`<%= escape_javascript(render partial: 'my_partial', locals: { id: ${id} }) %>`
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);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.