admin管理员组文章数量:1414621
I'm able to render partial inside a modal using escape_javascript
in js.erb file code:
$("body").append("<%= escape_javascript(render partial: 'example_partial') %>");
$('#my_modal').modal('show');
However, I can't seem to get results for:
console.log(<%= @error %>)
I'm able to render partial inside a modal using escape_javascript
in js.erb file code:
$("body").append("<%= escape_javascript(render partial: 'example_partial') %>");
$('#my_modal').modal('show');
However, I can't seem to get results for:
console.log(<%= @error %>)
Share
Improve this question
edited Jul 15, 2016 at 19:44
the Tin Man
161k44 gold badges221 silver badges306 bronze badges
asked Jul 15, 2016 at 9:21
bill_cosbybill_cosby
1452 silver badges9 bronze badges
2
-
1
You need to put inverted mas around the ruby helper.
console.log('<%= @error %>')
– Sinan Guclu Commented Jul 15, 2016 at 9:23 - That's right thanks. My bad – bill_cosby Commented Jul 15, 2016 at 9:28
1 Answer
Reset to default 5ERB will output a plain string. JS needs inverted mas around a string for it to be recognized. You have missed them on your console.log()
statement.
Change it to:
console.log('<%= @error %>');
You may also find the raw
helper useful. This will call .to_s
and .html_safe
on any erb output:
console.log('<%= raw @error %>');
Read more about html_safe
here.
本文标签: javascriptHow to use Ruby code inside jserbStack Overflow
版权声明:本文标题:javascript - How to use Ruby code inside js.erb? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745155892a2645168.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论