The following line of code:
$("#comments_<%[email protected] %>").append("<%= escape_javascript(render :partial => 'posts/comment', :locals => { :comment => @comment }) %>");
Is supposed to instert a partial as html inside the coments_xx div tag. what is happening is that the content of the partial is inserted but not interpreted as html, i mean, instead of inserting a comment with its right format i see the whole code in the web page:
Example (this is how it insert it in the webpage):
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!
If i analize the javascript code inserted i get something like this (i used firebug extension to see it):
/* 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>");
Finally this is the code of the partial that i am inserting:
<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>
Hope i could explain myself well!
thanks in advance for your help.
I found the solution!! Just having the rails code inside #{ }:
$("#comments_<%[email protected] %>").append("<%= escape_javascript("#{render :partial => 'posts/comment', :locals => { :comment => @comment }}").html_safe %>");
I had a similar problem a few days ago. It looks like you're running afoul of Rails making HTML output safe.
Try adding .html_safe to the end of the escape_javascript:
<%= escape_javascript(render :partial => 'posts/comment', :locals => { :comment => @comment }).html_safe %>
I'm not currently able to check, but if this is caused by the same sort of problem that mine was it should work for you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With