最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to use Ruby code inside js.erb? - Stack Overflow

programmeradmin1浏览0评论

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
Add a ment  | 

1 Answer 1

Reset to default 5

ERB 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.

发布评论

评论列表(0)

  1. 暂无评论