以下代码行:
$("#comments_<%=@comment.post.id %>").append("<%= escape_javascript(render :partial => 'posts/comment', :locals => { :comment => @comment }) %>");
应该在comments_xx div标签内插入部分html。发生的事情是插入了部分内容但没有解释为 html,我的意思是,我没有插入具有正确格式的评论,而是在网页中看到了整个代码:
示例(这是它在网页中插入的方式):
1 Comment
<div id=comment_5_34> <span class=dateandoptions> Posted less than a minute ago<br/>
<a href=/comments/34/5 data-method=post data-remote=true rel=nofollow>Deletea> span>
<p><b>otra pruebab> wrote:p> <br/> <p><b> Webpage:b>asss.comp> <br/> <p class=comment-body>heeyeyeyyhep>div>
Thanks for commenting!
如果我分析插入的 javascript 代码,我会得到类似这样的东西(我使用 firebug 扩展来查看它):
/* Add the new comment to the bottom of the comments list */
$("#comments_5").append("<div id=comment_5_34> <span class=dateandoptions> Posted less than a minute ago<br/> <a href=/comments/34/5 data-method=post data-remote=true rel=nofollow>Deletea> span> <p><b>otra pruebab> wrote:p> <br/> <p><b> Webpage:b>asss.comp> <br/> <p class=comment-body>heeyeyeyyhep>div>");
最后,这是我要插入的部分代码:
<div id="comment_<%=comment.post.id%>_<%=comment.id%>">
<span class="dateandoptions">
Posted <%= time_ago_in_words(comment.created_at) %> ago<br/>
<%= link_to 'Delete', {:controller => 'comments', :action => 'eliminar', :id => comment.id, :post_id => comment.post.id}, :method => :post, :remote => true %>
</span>
<p><b><%= comment.user_name %></b> wrote:</p>
<br/>
<% if comment.web_page != nil %> <p><b> Webpage:</b><%= comment.web_page %></p> <% end %>
<br/>
<%= content_tag(:p, comment.contenido, :class => "comment-body") %>
</div>
希望我能很好地解释自己!
在此先感谢您的帮助。