# Rendering multiline Javascript by using JSONP in rails

**URL:** <https://www.ruby-forum.com/t/rendering-multiline-javascript-by-using-jsonp-in-rails/192662>\
**Category:** Rails\
**Created:** [August 3, 2010, 7:49pm UTC](https://www.ruby-forum.com/t/rendering-multiline-javascript-by-using-jsonp-in-rails/192662 "2010-08-03T19:49:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![lucas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ba8739/32.png) [@lucas](https://www.ruby-forum.com/u/lucas)\
**Post date:** [August 3, 2010, 7:49pm UTC](https://www.ruby-forum.com/t/rendering-multiline-javascript-by-using-jsonp-in-rails/192662/1 "2010-08-03T19:49:44Z")

</div>

Hello everybody,

In a controller I try to render a javascript google ad to print it  
into some HTML using JSONP, like this

So I do:

render :text “var s\_response = { content: '\<script type="text/  
javascript"\>\<\/script\>\n\<script type="text/  
javascript"\n src="[http://pagead2.google.com/pgad/show\_ads.js\](http://pagead2.google.com/pgad/show_ads.js%5C)”\>  
\n\<\/script\>', id: ‘2’};"\>

But i get this error:

unterminated string literal

[Break on this error] var response = { content: '\<!–

=\> That seems like it is a multilines problem right ? But I don’t know  
how to resolve it.

Thanks,

Lucas

---

<div class="post-metadata">

**Author:** ![lucas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ba8739/32.png) [@lucas](https://www.ruby-forum.com/u/lucas)\
**Post date:** [August 3, 2010, 8:00pm UTC](https://www.ruby-forum.com/t/rendering-multiline-javascript-by-using-jsonp-in-rails/192662/2 "2010-08-03T20:00:16Z")

</div>

Lucas wrote:

> Hello everybody,
> 
> In a controller I try to render a javascript google ad to print it  
> into some HTML using JSONP, like this
> 
> So I do:
> 
> render :text “var s\_response = { content: '\<script type="text/  
> javascript"\>\<\/script\>\n\<script type="text/  
> javascript"\n src="[http://pagead2.google.com/pgad/show\_ads.js\](http://pagead2.google.com/pgad/show_ads.js%5C)”\>  
> \n\<\/script\>', id: ‘2’};"\>
> 
> But i get this error:
> 
> unterminated string literal
> 
> [Break on this error] var response = { content: '\<!–
> 
> =\> That seems like it is a multilines problem right ?

No. The problem is that your double-quoted string contains double  
quotes!

> But I don’t know  
> how to resolve it.

There are quoting constructs that would do the trick. But you have a  
bigger problem: you really shouldn’t be doing this in the controller.  
Rendered JS, like rendered HTML, belongs in the view.

> Thanks,
> 
> Lucas

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![lucas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ba8739/32.png) [@lucas](https://www.ruby-forum.com/u/lucas)\
**Post date:** [August 3, 2010, 8:48pm UTC](https://www.ruby-forum.com/t/rendering-multiline-javascript-by-using-jsonp-in-rails/192662/3 "2010-08-03T20:48:22Z")

</div>

> No. The problem is that your double-quoted string contains double  
> quotes!

It contains double-quotes but they are escaped aren’t they ?

> There are quoting constructs that would do the trick. But you have a  
> bigger problem: you really shouldn’t be doing this in the controller.  
> Rendered JS, like rendered HTML, belongs in the view.

In fact this code isn’t print in my HTML but in the HTML of another  
website, thiat is why it is in a controller and not a view

Thanks,

Lucas

---

<div class="post-metadata">

**Author:** ![lucas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ba8739/32.png) [@lucas](https://www.ruby-forum.com/u/lucas)\
**Post date:** [August 3, 2010, 9:29pm UTC](https://www.ruby-forum.com/t/rendering-multiline-javascript-by-using-jsonp-in-rails/192662/4 "2010-08-03T21:29:29Z")

</div>

Lucas wrote:

> > No. ï¿½The problem is that your double-quoted string contains double  
> > quotes!
> 
> It contains double-quotes but they are escaped aren’t they ?

Whoops! You’re right. I somehow overlooked the backslashes.

> > There are quoting constructs that would do the trick. ï¿½But you have a  
> > bigger problem: you really shouldn’t be doing this in the controller.  
> > Rendered JS, like rendered HTML, belongs in the view.
> 
> In fact this code isn’t print in my HTML but in the HTML of another  
> website, thiat is why it is in a controller and not a view

Doesn’t matter. If it goes to the user’s browser, it should generally  
be in a view.

> Thanks,
> 
> Lucas

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid
