[英]How do I list only one instance of each comment?
I have an admin dashboard that takes the comments from all of the blog posts and lists them so I can manage them easier. 我有一个管理仪表盘,该仪表盘可接收所有博客文章中的评论并列出它们,以便我更轻松地进行管理。 The problem is it shows a second instance of comments left as a reply which makes it a bit cluttered. 问题在于它显示了第二个评论实例作为答复,这使其显得有些混乱。 How do I stop it from listing replies twice? 如何阻止它两次列出答复? This isn't an issue when they render in the post's view. 当他们在帖子的视图中呈现时,这不是问题。
How I call the list in my user.show view: 我如何在user.show视图中调用列表:
<%= render(partial: 'comments/comment', collection: @comments) %>
users_controller (show method): @comments = Comment.all
users_controller(显示方法): @comments = Comment.all
_comment.html.erb partial: _comment.html.erb部分:
<div class="wellington top-drop">
<h3 class="title-top align-left"><%=h comment.name %><% if comment.admin_comment == true %><span class="text-muted"> | Admin</span><% end %></h3>
<% if current_user.present? && current_user.admin? %>
<%= link_to "Delete", comment, method: :delete,
data: { confirm: "Are you sure you want to delete this comment? This will delete all replies to this comment." },
class: "btn btn-xs btn-danger align-right" %>
<p class="align-right text-muted pad-right"><%= comment.updated_at.strftime('%b %d, %Y') %></p>
<% end %>
<div style="clear: both;"></div>
<p class="nobot align-left"><%=h comment.body %></p> <!-- the h breaks down html tags into plain text -->
<button type="button" class="btn btn-xs btn-success align-right" data-toggle="collapse" data-target="<%= "#collapse#{comment.id}" %>" aria-expanded="false" aria-controls="<%= "collapse#{comment.id}" %>">Reply</button>
<div style="clear: both;"></div>
<div class="collapse" id="<%= "collapse#{comment.id}" %>">
<%= simple_form_for([comment, Comment.new]) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= f.input :body, :as => :text, input_html: { maxlength: 300 }, label: false, placeholder: "What are your thoughts?", class: "form-control", wrapper_html: { id: 'contactTextarea' } %>
<%= f.input :name, label: false, placeholder: "Name (required) - Just your first name is okay too!", class: "form-control" %>
<%= f.input :email, label: false, placeholder: "Email Address (required) - This is not displayed with the comment", class: "form-control" %>
<div class="form-group hidden">
<%= f.input :nickname, :hint => "leave this field blank!", class: "form-control" %>
</div>
<%= f.submit "Reply", class: "btn btn-success" %>
<% end %>
</div>
</div>
<ul>
<%= render partial: 'comments/comment', collection: comment.comments %>
</ul>
This is what it looks like: 看起来是这样的:
EDIT: I don't have this issue when rendering them in their respective post's view - only when it's being rendered in the user's show view. 编辑:在他们各自的帖子的视图中呈现它们时,我没有这个问题-仅当在用户的显示视图中呈现它们时。
I am handling my comments through a polymorphic relation: 我正在通过多态关系处理我的评论:
CommentsController: CommentController:
before_action :find_commentable
private
def find_commentable
@commentable = Comment.find_by_id(params[:comment_id]) if params[:comment_id]
@commentable = Post.friendly.find(params[:post_id]) if params[:post_id]
end
Comment Model: 评论模型:
class Comment < ApplicationRecord
belongs_to :commentable, polymorphic: true
belongs_to :user, optional: true
has_many :comments, as: :commentable, dependent: :destroy
default_scope {order(created_at: :asc)}
attribute :nickname, :captcha => true
validates :body, presence: true, length: { minimum: 3, maximum: 300 }
validates :name, presence: true, length: { minimum: 2, maximum: 30 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i
validates :email, presence: true, length: { maximum: 100 },
format: { with: VALID_EMAIL_REGEX }
def admin_comment
user&.admin
end
end
After some testing, I was able to list my comments only once by using this in my users_controller: 经过一些测试,通过在users_controller中使用以下命令,我只能列出我的评论:
@comments = Comment.where(commentable_type: "Post")
Instead of using this: 而不是使用此:
@comments = Comment.all
This only listed comments that belonged to a post but also allowed comments that belonged to another comment (as a reply) to be listed under them. 这不仅列出了属于帖子的评论,而且还允许将属于另一个评论(作为回复)的评论列在其下。
If you don't want child comments to appear as if they are top level comments, you need to remove them from your @comments collection before rendering your partials. 如果您不希望子注释看起来像是顶级注释,则需要在呈现部分内容之前将它们从@comments集合中删除。 You're still rendering the child comments with 您仍然使用
<%= render partial: 'comments/comment', collection: comment.comments %>
So no need to render them as top level comments too. 因此,也无需将其呈现为顶级注释。
Instead of 代替
@comments = Comment.all
Try 尝试
@comments = Comment.where(comment_id: nil)
It's probably best to put something like that in the model, so maybe: 最好在模型中放入类似的内容,因此:
class Comment < ApplicationRecord
scope :top_level, -> { where(comment_id: nil) }
....
end
And in your controller: 在您的控制器中:
@comments = Comment.top_level
EDIT: This assumes comments have a comment_id to establish their parent comment 编辑:这假定注释具有comment_id以建立其父注释
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.