The following line of code:
$("#comments_<%=@comment.post.id %>").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 c开发者_Go百科lass=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_<%=@comment.post.id %>").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.
I tried the solution of SaucyK and it seems the way to go, but still i have an strange output rendered in my browser.
So now the line of code goes (as SaucyK proposed):
$("#comments_<%=@comment.post.id %>").append("<%= escape_javascript(render :partial => 'posts/comment', :locals => { :comment => @comment }).html_safe %>");
And this is what i get now in my browser:
Posted less than a minute ago
Deletea> span>
diego probandob> wrote:p>
Webpage:b>www.toteria.comp>
pruebaaaap>div>
If I look in the final html code generated by rails and Jquery I get this:
<div style="" id="comment_5_40">
<span class="dateandoptions"> Posted less than a minute ago<br> <a href="/comments/40/5" data-method="post" data-remote="true" rel="nofollow">Deletea> span> <p><b>diego probandob> wrote:p> <br> </b></p><p><b><b> Webpage:b>www.toteria.comp> <br> </b></b></p><p class="comment-body"><b><b>pruebaaaap>div></b></b></p></a></span></div></div>
and the comment i wrote was
diego probando wrote:
Webpage:www.toteria.com
pruebaaaa
When i refresh the webpage (so the commment is introduced through rails and not through Javascript) everything is fine.
So as you can see at the beginning the code is interpreted rigth but then there are some places where instead of getting a ' I get > or instead of getting i get b>.
Can it be related with the method escape_javascript ?? i understand it is necessary anyway if i don't want javascript to try interpreting my ruby code..
The code of the partial i am inserting is still the same as in the first post!.
Thanks for your answer, sorry i have to reply this why but i wasn't registered in stackoverflow when i wrote the question.
精彩评论