Forum: Ruby on Rails unterminated string literal, how to properly send strings

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
B0a92f29f123139709bd9f7bae6f8218?d=identicon&s=25 michael_teter (Guest)
on 2008-12-09 17:53
(Received via mailing list)
I have a line like the following in my Rails view/template (generating
JavaScript):

<script type="text/javascript">
...
y = escape('<%= h(e.error_desc) %>');
...
</script>

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?
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2008-12-09 18:02
(Received via mailing list)
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
B0a92f29f123139709bd9f7bae6f8218?d=identicon&s=25 michael_teter (Guest)
on 2008-12-09 18:39
(Received via mailing list)
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.
This topic is locked and can not be replied to.