Die*_*mpo 12 javascript jquery ruby-on-rails append partials
以下代码行:
$("#comments_<%=@comment.post.id %>").append("<%= escape_javascript(render :partial => 'posts/comment', :locals => { :comment => @comment }) %>");
应该在coments_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> 
希望我能很好地解释自己!
在此先感谢您的帮助.
我找到了解决方案!! 只需在#{}内部使用rails代码:
$("#comments_<%=@comment.post.id %>").append("<%= escape_javascript("#{render :partial => 'posts/comment', :locals => { :comment => @comment }}").html_safe %>");
几天前我也遇到了类似的问题。看来您在确保 HTML 输出安全方面与 Rails 发生了冲突。
尝试将 .html_safe 添加到 escape_javascript 的末尾:
<%= escape_javascript(render :partial => 'posts/comment', :locals => { :comment => @comment }).html_safe %>
我目前无法检查,但如果这是由与我的问题相同的问题引起的,那么它应该对您有用。
| 归档时间: | 
 | 
| 查看次数: | 8289 次 | 
| 最近记录: |