14

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("&lt;div id=comment_5_34&gt;    &lt;span    class=dateandoptions&gt;        Posted less than a minute ago&lt;br/&gt;        &lt;a href=/comments/34/5 data-method=post data-remote=true rel=nofollow&gt;Deletea&gt;    span&gt;    &lt;p&gt;&lt;b&gt;otra pruebab&gt; wrote:p&gt;    &lt;br/&gt;     &lt;p&gt;&lt;b&gt; Webpage:b&gt;asss.comp&gt;     &lt;br/&gt;    &lt;p class=comment-body&gt;heeyeyeyyhep&gt;div&gt;"); 

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.

Diego Ocampo
  • 141
  • 1
  • 3

3 Answers3

9

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 %>");
diegopau
  • 1,324
  • 10
  • 22
  • 1
    I'm still getting an undefined method render when I follow this suggestion. My example is pretty similar. Would you have an idea of what I might be missing? The only different between your answer here and what I have is the model and partial names. – Jake Smith Mar 22 '13 at 23:31
  • Also, the reason I am trying this method is because I have a list of partials that I create using an embedded ruby each loop on the current items. My goal is to append a newly created object upon submission to the current list without reloading the page. Do you know if there is a way to just refresh that particular ruby each loop? That would probably require an ajax call, but I figured it would be easy to do something like this: `$("#activeTab").append("<%= escape_javascript("#{render :partial => 'groups/thing-list-row', :locals => {:t => Thing.order(:created_at).last}}") %>");` – Jake Smith Mar 22 '13 at 23:33
  • 1
    For future reference of people finding this thread looking for answers: one common reason for getting "undefined method" on render is that you're using render in a file inside /assets rather than app/views. – Steve Jan 26 '16 at 05:59
3

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.

Dave A-R
  • 1,159
  • 1
  • 11
  • 22
0

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&gt;    span&gt;    <p><b>diego probandob&gt; wrote:p&gt;    <br>     </b></p><p><b><b> Webpage:b&gt;www.toteria.comp&gt;     <br>    </b></b></p><p class="comment-body"><b><b>pruebaaaap&gt;div&gt;</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.

diegopau
  • 1,324
  • 10
  • 22