如何在不重新加载页面的情况下显示评论?

时间:2019-07-18 09:18:53

标签: ruby-on-rails mongoid

我使用Ruby on Rails 5.2和Mongoid

当我添加新评论时,它没有出现,我必须重新加载页面才能看到它

我不明白它为什么不起作用的原因

日志:

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"=>"✓", "comment"=>{"text"=>"Test comment"}, "commit"=>"Post", "post_id"=>"alert-test-1"}
MONGODB | localhost:27017 | remotely_development.find | STARTED | {"find"=>"posts", "filter"=>{"_slugs"=>{"$in"=>["alert-test-1"]}}, "limit"=>1, "sort"=>{"created_at"=>-1}, "lsid"=>{"id"=><BSON::Binary:0x70324575353440 type=uuid data=0xde9d0a99e1174026...>}}
MONGODB | localhost:27017 | remotely_development.find | SUCCEEDED | 0.001s
MONGODB | localhost:27017 | remotely_development.find | STARTED | {"find"=>"users", "filter"=>{"_id"=>BSON::ObjectId('5d23bc2b1996da03ae5735e7')}, "sort"=>{"_id"=>1}, "limit"=>1, "singleBatch"=>true, "lsid"=>{"id"=><BSON::Binary:0x70324575353440 type=uuid data=0xde9d0a99e1174026...>}}
MONGODB | localhost:27017 | remotely_development.find | SUCCEEDED | 0.001s
MONGODB | localhost:27017 | remotely_development.find | STARTED | {"find"=>"users", "filter"=>{"_id"=>BSON::ObjectId('5d23bc2b1996da03ae5735e7')}, "limit"=>1, "singleBatch"=>true, "lsid"=>{"id"=><BSON::Binary:0x70324575353440 type=uuid data=0xde9d0a99e1174026...>}}
MONGODB | localhost:27017 | remotely_development.find | SUCCEEDED | 0.001s
MONGODB | localhost:27017 | remotely_development.insert | STARTED | {"insert"=>"comments", "ordered"=>true, "documents"=>[{"_id"=>BSON::ObjectId('5d3038e61996da7af313fa4b'), "text"=>"Test comment", "commentable_id"=>BSON::ObjectId('5d2c8f321996da09862539f8'), "commentable_type"=>"Post", "user_id"=>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"=>"comments", "filter"=>{"commentable_id"=>BSON::ObjectId('5d2c8f321996da09862539f8'), "commentable_type"=>"Post"}, "lsid"=>{"id"=><BSON::Binary:0x70324575353440 type=uuid data=0xde9d0a99e1174026...>}}
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)

create.js.erb

$(".comments").html("<%= j render @commentable.comments %>")
$("textarea").val("")

_comment.html.erb

<li id="<%= comment.id %>">
  <%= render partial: 'comments/single_comment', locals: { comment: comment } %>
</li>

_single_comment.html.erb

<span class="user"><%= link_to comment.user.name, comment.user %></span>
<span class="content"><%= comment.text %></span>
<div class="timestamp">
  <%= time_ago_in_words(comment.created_at) %> ago.
</div>
<% if current_user?(comment.user) %>
  <%= render partial: 'comments/delete_comment_form', locals: { comment: comment } %>
<% end %>

comments / show.html.erb

<div class="comments">
<%= render partial: 'comments/single_comment', locals: { comment: @comment } %>
</div>

posts / show.html.erb

<%= render partial: 'comments/comment_form', locals: { commentable: @post } %>
<%= render @post.comments %>

1 个答案:

答案 0 :(得分:0)

此行

$(".comments").html("<%= j render @commentable.comments %>")

要求页面上至少有一个元素与CSS选择器.comments相匹配。如果没有这样的元素,则此行无效。