简体   繁体   中英

How do I add a rails controller variable to javascript file console.log?

I have added a variable to a rails controller.

@fart = "I farted"

I added a file to the assets/javascripts folder called fart.js.erb (for preprocessing)

The file contains one line

console.log("<%=@fart%>");

My expected result would be that it prints "I farted" to the console. Instead, it prints a blank line.

I have tried different variations of

console.log("<%='@fart'%>");
console.log("<%=@fart.html_safe%>");
console.log("<%=@fart%>");

none of which work. How do I print my fart correctly?

See if this smells better:

console.log('<%=@fart%>');

You don't need the double-quotes for interpolation to work in a .erb file.

尝试:

console.log("<%=@fart.to_json%>");

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