Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy does this ajax request replace all my comments with the newly submitted comment?
    text
    copied!<p>So I have this form which requests a JS call when a comment is submitted:</p> <pre><code>&lt;%= simple_form_for([@video, @video.comments.new], :remote =&gt; true) do |f| %&gt; &lt;%= f.association :comment_title, :collection =&gt; @video.comment_titles, :label =&gt; "Comment Title:", :include_blank =&gt; false %&gt; &lt;%= f.input :body, :label =&gt; false, :placeholder =&gt; "Post a comment." %&gt; &lt;%= f.button :submit, :value =&gt; "Post" %&gt; &lt;% end %&gt; </code></pre> <p>It calls this create action in the comments controller:</p> <pre><code>def create @comment = @video.comments.new(params[:comment].merge({:user_id =&gt; current_user.id})) if @comment.save respond_to do |format| format.html { redirect_to :back } format.js end else respond_to do |format| format.html { redirect_to :back, :alert =&gt; "Unable to add comment." } format.js { render 'fail_create.js.erb' } end end end </code></pre> <p>which renders this create.js.erb file:</p> <pre><code>$(".comment_content").html('&lt;%= escape_javascript(render(@comment)) %&gt;'); $(".comment_form")[0].reset(); </code></pre> <p>The above create.js.erb file renders the comment partial into this file:</p> <pre><code>&lt;% comment_title.comments.each do |comment| %&gt; &lt;div class="comment_content"&gt; &lt;%= render comment %&gt; &lt;/div&gt; &lt;% end %&gt; </code></pre> <p>Why are all of my comments being replaced by the newly submitted comment in this AJAX request?</p> <p>Here's my comment partial:</p> <pre><code>&lt;%= link_to image_tag(comment.user.profile.photo.url(:tiny)), profile_path(comment.user.profile), :class =&gt; "comment_image" %&gt; &lt;div class="textual_comment_content"&gt; &lt;div class="comment_text"&gt; &lt;span class="name_link"&gt; &lt;%= link_to "#{comment.user.name}", profile_path(comment.user.profile), :class =&gt; "normal" %&gt; &lt;/span&gt; &lt;%= comment.body.gsub("'",'&amp;apos;').html_safe %&gt; &lt;/div&gt; &lt;span class="comment_footer"&gt; &lt;ul&gt; &lt;li class="list_style"&gt;&lt;%= time_ago_in_words(comment.created_at) %&gt; ago&lt;/li&gt; &lt;% unless current_user != comment.user %&gt; &lt;li&gt;&lt;%= link_to "Delete", video_comment_path(:video_id =&gt; @video, :id =&gt; comment), :method =&gt; :delete, :class =&gt; "normal" %&gt;&lt;/li&gt; &lt;% end %&gt; &lt;/ul&gt; &lt;/span&gt; &lt;/div&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload