简体   繁体   中英

How to render html.erb file to assets/javascript?

Below is the code I have in my assets/javascripts/investment.js file:

investment_updates.forEach(function(investment_update, n){
    data.push(new Item((n+1)*space, (n+1)*space, (n)*angle));
    add(n+1, "<%= render '/investment_updates/single_investment_update', investment_update: investment_update %>")
});

I want to render views/investment_updates/_single_investment_update.html.erb file in my javascript file and I tried below code but didn't work.

<%= render '/investment_updates/single_investment_update', investment_update: investment_update %>

Help me to render partial in my js file as a parameter.

Your js file should end in .js.erb instead, so it will be picked up by the Rails Asset Pipeline.

Note though that the way you are inserting data in a string is quite brittle. One misplaced ' or " can break your Javascript

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM