# Unterminated string literal, how to properly send strings

**URL:** <https://www.ruby-forum.com/t/unterminated-string-literal-how-to-properly-send-strings/155232>\
**Category:** Rails\
**Created:** [December 9, 2008, 5:53pm UTC](https://www.ruby-forum.com/t/unterminated-string-literal-how-to-properly-send-strings/155232 "2008-12-09T17:53:55Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![michael\_teter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e47c2d/32.png) [@michael\_teter](https://www.ruby-forum.com/u/michael_teter)\
**Post date:** [December 9, 2008, 5:53pm UTC](https://www.ruby-forum.com/t/unterminated-string-literal-how-to-properly-send-strings/155232/1 "2008-12-09T17:53:55Z")

</div>

I have a line like the following in my Rails view/template (generating  
JavaScript):

Because some of the error\_descs have newlines, the browser is  
receiving page code that looks like this:

y = escape(‘Information about the error.  
Another line in the description.  
A third line in the description string.’);

JavaScript is choking on the broken string with the error  
“unterminated string literal”. What is the best practices way to  
handle this, since apparently Rails’s h() isn’t removing newlines?

---

<div class="post-metadata">

**Author:** ![michael\_teter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e47c2d/32.png) [@michael\_teter](https://www.ruby-forum.com/u/michael_teter)\
**Post date:** [December 9, 2008, 6:02pm UTC](https://www.ruby-forum.com/t/unterminated-string-literal-how-to-properly-send-strings/155232/2 "2008-12-09T18:02:46Z")

</div>

On 9 Dec 2008, at 16:52, michael\_teter wrote:

> Because some of the error\_descs have newlines, the browser is  
> receiving page code that looks like this:
> 
> y = escape(‘Information about the error.  
> Another line in the description.  
> A third line in the description string.’);

to\_json ?

Fred

---

<div class="post-metadata">

**Author:** ![michael\_teter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e47c2d/32.png) [@michael\_teter](https://www.ruby-forum.com/u/michael_teter)\
**Post date:** [December 9, 2008, 6:39pm UTC](https://www.ruby-forum.com/t/unterminated-string-literal-how-to-properly-send-strings/155232/3 "2008-12-09T18:39:11Z")

</div>

I believe I found the solution I was looking for - escape\_javascript  
().

Seems obvious now, but somehow I hadn’t encountered it (and amazingly,  
none of my searches for “rails javascript unterminated literal string”  
turned up this useful function. It’s part of the Rails JavaScript  
helpers.
