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

I'm able to render partial inside a modal using escape_javascript in js.erb file code:$("body

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745155892a4614117.html

相关推荐

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

    I'm able to render partial inside a modal using escape_javascript in js.erb file code:$("body

    5小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信