michael_teter
2008-Dec-09 16:52 UTC
unterminated string literal, how to properly send strings
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?
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2008-Dec-09 17:02 UTC
Re: unterminated string literal, how to properly send strings
On 9 Dec 2008, at 16:52, michael_teter wrote:> > 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.''); >to_json ? Fred> 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? > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
michael_teter
2008-Dec-09 17:38 UTC
Re: unterminated string literal, how to properly send strings
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. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---