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:
<%= render(partial: 'comments/comment', collection: @comments) %>
users_controller (show method): @comments = Comment.all
_comment.html.erb partial:
<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:
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:
@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.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments