我的设置:Rails 3.0.9,Ruby 1.9.2,jQuery 1.6.2
HTML
<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'
返回我希望在<div>...</div>
之前插入的textarea
。我发现输出中转义的特殊字符不能被JS正确呈现?这就是insertBefore
<div class="\"comment\"">(Bob less than a minute ago) beautiful day<\/div></div>
我只是注意到在输出结尾添加了额外的</div>
,似乎是jQuery,因为这是create.js.erb
吐出的内容,如Firebug控制台响应所示
'<div class=\"comment\">(Bob less than a minute ago) beautiful day<\/div>'
答案 0 :(得分:1)
尝试以下方法:
<%= raw escape_javascript( render 'show_comments') %>