2011-08-31 3 views
0

내 설정에서 출력을 탈출 표시 : HTMLjQuery를 레일

루비 1.9.2, jQuery를 1.6.2, 레일 3.0.9

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

jQuery를

$('#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"); 
    } 
}); 

레일

comments_controller.rb  
    def create 
    @comment = Comment.new 

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

create.js.erb 
"<%= escape_javascript(render 'show_comments') %>" 

render 'show_comments' ret 내가 textarea 전에 삽입하고자하는 <div>...</div>을 넣으십시오. 내가 발견 한 것은 출력에서 ​​이스케이프 된 특수 문자가 JS, 아이디어에 의해 제대로 렌더링되지 않는다는 것입니다. 이것은이 방화범 콘솔 응답과 같이 뱉어 무엇 create.js.erb 때문에 난 그냥 여분의 </div>이 보이는 jQuery를에 의해, 출력의 끝에 추가됩니다 것으로 나타났습니다 insertBefore

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

후 HTML 현재 모습입니다

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

답변

1

다음보십시오 :이 같은 결과를 가지고,

<%= raw escape_javascript(render 'show_comments') %> 
+0

좋은 제안을하지만이 작동하지 않았다, 나는 한때를 업데이트 출력과 함께 estion. – Bob