I want to have my Javascript inject some HTML on the fly but I hate to put HTML in my js because it belongs in the view, not the public folder. I thought I could be slick and do something like this in a view to get the HTLM stored in a js variable:
<script type='text/javascript'>
htmlVar = '<%= render :partial => "some/partial" %>';
</script>
Unfortunately this gets screwed up by the line-returns in the partial. Is there a way to make this work or achieve a similar result using RJS or the Minus-R plugin?
People succeed in answering ParagramStudios's questions 26% of the time (9 successes in 34 attempts).
Answers by: darksanity | Rich Collins | vlambert
<script type='text/javascript'> htmlVar = '<%= render_to_string(:partial => "some/partial").gsub(/\n/, '\n') %>'; </script>
The solution is to render your partial as a string and then process this string to escape the carriage returns.
Hope it works for you ;).