title: How to make comments appear without reloading the page?
body: <p>I use Ruby on Rails 5.2 and Mongoid</p>
  
  <p>When I add a new comment it does not appear, I have to reload the page to see it</p>
  
  <p>I can't understand what is the reason why it does not work</p>
  
  <p>log:</p>
  
  <pre><code>Started POST "/stories/alert-test-1/comments" for ::1 at 2019-07-18 12:16:22 +0300
  Processing by CommentsController#create as JS
    Parameters: {"utf8"=&gt;"✓", "comment"=&gt;{"text"=&gt;"Test comment"}, "commit"=&gt;"Post", "post_id"=&gt;"alert-test-1"}
  MONGODB | localhost:27017 | remotely_development.find | STARTED | {"find"=&gt;"posts", "filter"=&gt;{"_slugs"=&gt;{"$in"=&gt;["alert-test-1"]}}, "limit"=&gt;1, "sort"=&gt;{"created_at"=&gt;-1}, "lsid"=&gt;{"id"=&gt;&lt;BSON::Binary:0x70324575353440 type=uuid data=0xde9d0a99e1174026...&gt;}}
  MONGODB | localhost:27017 | remotely_development.find | SUCCEEDED | 0.001s
  MONGODB | localhost:27017 | remotely_development.find | STARTED | {"find"=&gt;"users", "filter"=&gt;{"_id"=&gt;BSON::ObjectId('5d23bc2b1996da03ae5735e7')}, "sort"=&gt;{"_id"=&gt;1}, "limit"=&gt;1, "singleBatch"=&gt;true, "lsid"=&gt;{"id"=&gt;&lt;BSON::Binary:0x70324575353440 type=uuid data=0xde9d0a99e1174026...&gt;}}
  MONGODB | localhost:27017 | remotely_development.find | SUCCEEDED | 0.001s
  MONGODB | localhost:27017 | remotely_development.find | STARTED | {"find"=&gt;"users", "filter"=&gt;{"_id"=&gt;BSON::ObjectId('5d23bc2b1996da03ae5735e7')}, "limit"=&gt;1, "singleBatch"=&gt;true, "lsid"=&gt;{"id"=&gt;&lt;BSON::Binary:0x70324575353440 type=uuid data=0xde9d0a99e1174026...&gt;}}
  MONGODB | localhost:27017 | remotely_development.find | SUCCEEDED | 0.001s
  MONGODB | localhost:27017 | remotely_development.insert | STARTED | {"insert"=&gt;"comments", "ordered"=&gt;true, "documents"=&gt;[{"_id"=&gt;BSON::ObjectId('5d3038e61996da7af313fa4b'), "text"=&gt;"Test comment", "commentable_id"=&gt;BSON::ObjectId('5d2c8f321996da09862539f8'), "commentable_type"=&gt;"Post", "user_id"=&gt;BSON::ObjectId('5d23...
  MONGODB | localhost:27017 | remotely_development.insert | SUCCEEDED | 0.000s
    Rendering comments/create.js.erb
  MONGODB | localhost:27017 | remotely_development.find | STARTED | {"find"=&gt;"comments", "filter"=&gt;{"commentable_id"=&gt;BSON::ObjectId('5d2c8f321996da09862539f8'), "commentable_type"=&gt;"Post"}, "lsid"=&gt;{"id"=&gt;&lt;BSON::Binary:0x70324575353440 type=uuid data=0xde9d0a99e1174026...&gt;}}
  MONGODB | localhost:27017 | remotely_development.find | SUCCEEDED | 0.001s
    Rendered comments/_delete_comment_form.html.erb (1.4ms)
    Rendered comments/_single_comment.html.erb (8.6ms)
    Rendered collection of comments/_comment.html.erb [1 times] (12.9ms)
    Rendered comments/create.js.erb (21.3ms)
  Completed 200 OK in 83ms (Views: 61.5ms | MongoDB: 0.0ms)
  </code></pre>
  
  <p>create.js.erb</p>
  
  <pre><code>$(".comments").html("&lt;%= j render @commentable.comments %&gt;")
  $("textarea").val("")
  </code></pre>
  
  <p>_comment.html.erb</p>
  
  <pre><code>&lt;li id="&lt;%= comment.id %&gt;"&gt;
    &lt;%= render partial: 'comments/single_comment', locals: { comment: comment } %&gt;
  &lt;/li&gt;
  </code></pre>
  
  <p>_single_comment.html.erb</p>
  
  <pre><code>&lt;span class="user"&gt;&lt;%= link_to comment.user.name, comment.user %&gt;&lt;/span&gt;
  &lt;span class="content"&gt;&lt;%= comment.text %&gt;&lt;/span&gt;
  &lt;div class="timestamp"&gt;
    &lt;%= time_ago_in_words(comment.created_at) %&gt; ago.
  &lt;/div&gt;
  &lt;% if current_user?(comment.user) %&gt;
    &lt;%= render partial: 'comments/delete_comment_form', locals: { comment: comment } %&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>comments/show.html.erb</p>
  
  <pre><code>&lt;div class="comments"&gt;
  &lt;%= render partial: 'comments/single_comment', locals: { comment: @comment } %&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>posts/show.html.erb</p>
  
  <pre><code>&lt;%= render partial: 'comments/comment_form', locals: { commentable: @post } %&gt;
  &lt;%= render @post.comments %&gt;
  </code></pre>