title: submit edited comment in same place with ajax, jquery in rails
body: <p>I just tried a lot and search for answers but found nothing....
  I have comments in a post and user can edit their comments..
  if he click the edit in the comment, a form will display in place of the comment...all of that is working but when you submit the updates the comment will go below the list of the comments in the same post, and the edit form will stay and if he submit it again another (dublicated) one will go below of the list...now if I refresh the page the updated comment will back to the right place and the dublicated one will get removed...
  how to solve that? I want to replace the form with the updated commetns instead and without refresh...</p>
  
  <p>here is my post footer where the comments are desiplayed:</p>
  
  <pre><code>&lt;div class="panel-footer" id="panel_footer_&lt;%= post.id %&gt;"&gt;
    &lt;div class="comment-form" id="comment-form-j"&gt;
      &lt;%= render 'comment2', post: post, comment: post.comments.build %&gt;
    &lt;/div&gt;
    &lt;div class="comments" id= "comments_&lt;%= post.id %&gt;"&gt;
      &lt;% if post.comments.present? %&gt;
        &lt;%= render post.comments, post: post %&gt;
      &lt;% end %&gt;
    &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>here is my comment partial _comment.html.erb :</p>
  
  <pre><code>&lt;% if  comment.user_id.present? %&gt;
    &lt;div id="current_comment_&lt;%= comment.id %&gt;" &gt;
      &lt;div id="comment"&gt;
          &lt;div id="avatar_comment_&lt;%= comment.user_id %&gt;"&gt;
              &lt;%= link_to image_tag(comment.user.avatar.url, size: "30x30", class: "img-circle img-comments"), profile_path(comment.user.user_name) %&gt;
          &lt;/div&gt;
          &lt;div class="user-name"&gt;
              &lt;%= link_to comment.user.user_name, profile_path(comment.user.user_name), class: "username-size" %&gt;
          &lt;/div&gt;
          &lt;div class="comment-content comment-with-menu menu-comment-line" id = "this_comment_&lt;%= comment.id %&gt;" &gt;
            &lt;%= comment.content %&gt;
          &lt;/div&gt;
          &lt;% if comment.user == current_user %&gt;
            &lt;ul class="comment-menuu menu-comment-line"&gt;
              &lt;li class="dropdown"&gt;
                &lt;a href="#" class="dropdown-toggle menu-right-comment  " data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false"&gt; &lt;span class="caret"&gt;&lt;/span&gt;&lt;/a&gt;
                &lt;ul class="dropdown-menu"&gt;
                  &lt;li&gt;&lt;%= link_to "Edit", edit_post_comment_path(post, comment), remote: true %&gt;&lt;/li&gt;
                  &lt;li role="separator" class="divider"&gt;&lt;/li&gt;
                  &lt;li&gt;&lt;%= link_to "delete", post_comment_path(post, comment), method: :delete, data: { confirm: "Are you sure?" }, remote: true %&gt;&lt;/li&gt;
                &lt;/ul&gt;
              &lt;/li&gt;
            &lt;/ul&gt; 
          &lt;% end %&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>here is my comment form under posts(_comment2.html.erb): </p>
  
  <pre><code>&lt;%= form_for([post, comment], remote: true) do |f| %&gt;
    &lt;%= f.text_field :content, placeholder: 'Add a comment...', class: "form-control comment_content", id: "comment_content_#{post.id}"  %&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>here is my edit.js.erb under comments:</p>
  
  <pre><code>$("#this_comment_&lt;%= @comment.id %&gt;").replaceWith("&lt;%= j render 'posts/comment2', post: @post, comment: @comment %&gt;");
  </code></pre>
  
  <p>here is my update.js.erb under comments:</p>
  
  <pre><code>$('#comments_&lt;%= @post.id %&gt;').append("&lt;%=j render 'comments/comment', post: @post, comment: @comment, remote: true %&gt;");
  </code></pre>
  
  <p>and here is the comment controller where the edit and update:</p>
  
  <pre><code>def edit
    @comment = @post.comments.find(params[:id])
    respond_to do |format|
      format.js 
    end
  end
  
  def update
    @comment = @post.comments.find(params[:id])   
  
    if @comment.user_id == current_user.id
       if @comment.update(comments_params)
         respond_to do |format|
           format.html {redirect_to root_path}
           format.js 
          end
       else
         flash[:alert] = "Something worng, try again"
         render root_path
       end
    end
  end
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/8uR5F.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/8uR5F.png" alt="enter image description here"></a></p>