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.