简体   繁体   English

创建后如何使用Ajax仅显示第一条评论

[英]How to use Ajax to show first and only one comment as soon as it is created

At present my application is able to append comments to microposts that already have comments. 目前,我的应用程序能够将评论添加到已经具有评论的微博中。 Below is _micropost.html.erb (simplified): 以下是_micropost.html.erb (简体):

<li id="micropost-<%= micropost.id %>">
  <span class="content">
    <%= micropost.content %>
  </span>
  <span class="timestamp">
    Posted <%= time_ago_in_words(micropost.created_at) %> ago.
      ~ <%= link_to "Comment", "#", class: "comment-link", remote: true %>
    <% if micropost.comments.any? %>
      ~ <%= link_to "Show/hide comments", "#", class: "comments-link", remote: true %>
    <% end %>
  </span>
  <% if logged_in? && (current_user == micropost.user || current_user.friend?(micropost.user)) %>
    <div class="comment-section">
      <%= form_for(current_user.comments.build, remote: true) do |f| %>
        <%= f.hidden_field :micropost_id, value: micropost.id %>
        <%= f.text_area :content, rows: "1", class: "comment_area" %>
        <%= f.submit "Comment", class: "btn btn-primary btn-sm" %>
      <% end %>
    </div>
  <% end %>
  <div class="comments-section">
    <% if micropost.comments.any? %>
      <ol id="comments_micropost-<%= micropost.id %>">
        <% micropost.comments.each do |comment| %>
          <%= render comment %>
        <% end %>
      </ol>
    <% end %>
  </div>
</li>

create.js.erb is: create.js.erb是:

var comments = $('ol#comments_micropost-<%= @micropost.id %>');
comments.append('<%= escape_javascript(render partial: @comment) %>');

As it is conceived, create.js.erb implies that the comment created is added to other comments, that is that the comment created is not the first one. 在构思时, create.js.erb意味着将创建的注释添加到其他注释中,也就是说,创建的注释不是第一个。 In this case, var comments is not null and the last line of code append the comment to the list of the other comments. 在这种情况下, var comments不为null,并且代码的最后一行将注释附加到其他注释的列表中。
Also, in case micropost.comments is not nil, the user can use the "Show/hide comments" link to toggle the order list with id="comments_micropost-<%= micropost.id %>" 另外,如果micropost.comments不为零,则用户可以使用“显示/隐藏评论”链接来切换id="comments_micropost-<%= micropost.id %>"的订单。

The problem with this configuration is that in case a user add to any micropost the first comment (that is the user writes his comment when micropost.comments == 0 ) there is no chance to see the result without refreshing the page. 这种配置的问题在于,如果用户向任何微博添加了第一个评论(也就是说,当micropost.comments == 0时用户写了他的评论),就没有机会不刷新页面就看到结果。

So I am asking: how can I edit create.js.erb so that the user can see straight away the result of posting the first comment and that the "Show/hide comments" link be added to the page? 所以我问:如何编辑create.js.erb以便用户可以立即看到发布第一个评论的结果,并将“显示/隐藏评论”链接添加到页面?

I tried the following code but it does not work: 我尝试了以下代码,但它不起作用:

if (comments !== null) {
    comments.append('<%= escape_javascript(render partial: @comment) %>');
} else {
        $('#micropost-<%= @micropost.id %>').find('.comments-section').append("<ol id='comments_micropost-<%= @micropost.id %>'><%= escape_javascript(render partial: @comment) %></ol>");
        $('#micropost-<%= @micropost.id %>').find('.timestamp').append(" ~ <%= link_to 'Show/hide comments', '#', class: 'comments-link', remote: true %>");
};
<li id="micropost-<%= micropost.id %>">
  <span class="content">
    <%= micropost.content %>
  </span>
  <span class="timestamp">
    Posted <%= time_ago_in_words(micropost.created_at) %> ago.
      ~ <%= link_to "Comment", "#", class: "comment-link", remote: true %>
    <% if micropost.comments.any? %>
      ~ <%= link_to "Show/hide comments", "#", class: "comments-link", remote: true %>
    <% end %>
  </span>
  <% if logged_in? && (current_user == micropost.user || current_user.friend?(micropost.user)) %>
    <div class="comment-section">
      <%= form_for(current_user.comments.build, remote: true) do |f| %>
        <%= f.hidden_field :micropost_id, value: micropost.id %>
        <%= f.text_area :content, rows: "1", class: "comment_area" %>
        <%= f.submit "Comment", class: "btn btn-primary btn-sm" %>
      <% end %>
    </div>
  <% end %>
  <div class="comments-section">
     <%= render partial: comments, micropost: micropost %>
  </div>
</li>    

And in _comments.html.erb 并在_comments.html.erb中

<% if micropost.comments.any? %>
   <ol id="comments_micropost-<%= micropost.id %>">
   <% micropost.comments.each do |comment| %>
      <%= render comment %>
   <% end %>
   </ol>
<% end %>

And in create.js.erb 并在create.js.erb中

var comments = $('ol#comments_micropost-<%= @micropost.id %>');
if (comments == undefined) {
  $('div#comments-section').html('<%= escape_javascript(render partial: comments, micropost: @micropost) %>');
} 
else {
  comments.append('<%= escape_javascript(render partial: @comment) %>');
};

I solved my issue creating as originally suggested by Minu a app/views/comments/_comments.html.erb file as follows: 我按照Minu最初建议的app/views/comments/_comments.html.erb文件解决了创建问题,如下所示:

<% if @micropost.comments.any? %>
  <ol id="comments_micropost-<%= @micropost.id %>">
    <% @micropost.comments.each do |comment| %>
      <%= render comment %>
    <% end %>
  </ol>
<% end %>

Without changing partial _micropost.html.erb I edited create.js.erb as follows: 在不更改部分_micropost.html.erb我对create.js.erb进行了如下编辑:

var comments = $('ol#comments_micropost-<%= @micropost.id %>');

if (comments.length == 0) {
  $('#micropost-<%= @micropost.id %>').find('.comments-section').html("<%= escape_javascript(render partial: 'comments/comments') %>");
}
else {
  comments.append('<%= escape_javascript(render partial: @comment) %>');
};

When micropost.comments.any? 什么时候micropost.comments.any? is false, var comments is neither null or undefined , but is considered as an empty array, as can be verified by using a web browser console. 如果为false,则var comments不能为nullundefined ,但是可以视为空数组,可以使用网络浏览器控制台进行验证。 Hence the use of if (comments.length == 0) . 因此,使用if (comments.length == 0) See post at Stackoverflow. 请参阅Stackoverflow上的帖子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM