Take the 2-minute tour ×

My setup: Rails 3.0.9, Ruby 1.9.2, jQuery 1.6.2


<textarea id="photo-42-9" class="comment_box">Write a comment...</textarea>


$('#newsfeed').delegate('.comment_box', 'keydown', function (event){
   if(event.keyCode == 13) {
    var $this = $(event.target);
    $.post('/comments', { title: ..., description: ... }, function(response) { $(response).insertBefore($this); }, "script");


  def create
    @comment = Comment.new

    respond_to do |format|
      format.html # new.html.erb

"<%= escape_javascript( render 'show_comments') %>"

render 'show_comments' returns a <div>...</div> that I wish to insert before textarea. What I found is that the special characters escaped in the output aren't rendered properly by JS, ideas? This is what it looks like currently in HTML after insertBefore

<div class="\&quot;comment\&quot;">(Bob less than a minute ago) beautiful day<\/div></div>

I just noticed that an extra </div> is added to the end of the output, seemingly by jQuery because this is what create.js.erb spits out as shown in Firebug console response

'<div class=\"comment\">(Bob less than a minute ago) beautiful day<\/div>'
share|improve this question
add comment

1 Answer

Try the following:

<%= raw escape_javascript( render 'show_comments') %>
share|improve this answer
Good suggestion but that didn't work, it had the same result, I have updated my question with the output. –  Bob Aug 31 '11 at 19:51
add comment

Your Answer


