Error when trying to send JSON from rails to javascript/jQuery

Hello,

I'm trying to send the code for a google ad from my server to a web
browser. The code is not embedded in the original HTML of the webpage.
The code is sent to the browser while page is rendering. We are
sending the google code on-the-fly like this because we have our own
ad targeting.

We have a controller method on our server ("foo") that sends this
reply to the browser:

    render :text => "var response = { content: '#{ad_code}', id:
'#{div_where_the_ad_will_appear}' };"

This method is called by our javascript code on the client-side:

    $.getScript( url_of_foo_method, callback_function );

The $.getScript function allows us to perform an ajax call (using
JSONP, not regular JSON) to our server.

When the adcode is just one line and is regular HTML, like this:
    ad_code = "<a href=\"http://adsrv1.com/a/?902/51220/CD8665/225400/
&optionalinfo=\"><img src=\"http://adsrv2.com/ai/?
902/51220/8665/225400\" alt=\"\" border=\"0\"></a>"

...then everything works fine.

But when the ad_code has google adsense code in it, which uses script
tags and is multiple lines, like this:

    ad_code = "<script type="text/javascript"><!--
    ad_stuff1 = "value1";
    ad_stuff2 = "value2";
    //--><\/script>
    <script type="text/javascript"
      src="http://adstuff/js_file.js">
    <\/script>"

...we get this error in the Firebug debugger:

    unterminated string literal
    [Break on this error] var response = { content: '<script
type="text/javascript"><!--
    ad_stuff1 = "value1";
    ad_stuff2 = "value2";
    //--><\/script>
    <script type="text/javascript"
      src="http://adstuff/js_file.js">
    <\/script>
    ', id: 'id1' };

The google ad_code is not all on one line, but as you can see in the
error, is multiple lines. So there is an unterminated string literal,
perhaps inside the google_ad_code we put there. I'm trying to
understand if the quotes and newlines in the google ad_code are
somehow causing a problem. Perhaps the quotes are being escaped wrong
because this is inside javascript and we're using JSONP?

I've spent all day trying to fix this, to no avail.
Maybe only the first line "var response = { content: '<script
type="text/javascript"><!--"
is processed by javascript. Maybe the end of line character breaks the
string. I don't know.

In the controller, I've added these 2 lines before the render :
code = code.gsub(/\n/,"\\n")
code = code.gsub(/\/script/,"\\/script")
So as to avoid the possible end of line string breaking. I gsubbed the
script tag for the same reasons.

But these fixes do nothing.
Does anyone have any ideas or advice ?

Thanks