How do I list only one instance of each comment?

Jake

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:

enter image description here

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
Jake

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.

edited at
0

Comments

0 comments
Login to comment

Related

Using Python, how do I split on multiple delimiters and keep only one in my output list?

How do I make sure to initialize only one Instance but from another class?

How do I remove only one instance from a list in Python?

I have a list of tuples and want to make a list that has only one instance of one of the strings in the tuple

How do I only allow one instance of an SKSpriteNode on screen at one time?

How does Ruby ensure only one instance of each number?

How do I get only one result for each app instead of double?

How do I convert each line elements into an Array, So I can select only one element like data[2]

How do I mutate a list-column to a common one leaving only the last value when there is a vector in the list?

How do I make the last column of my output include only each element of the list instead of the whole list?

How can I query only one instance of a label?

How can I use Redux to only update one instance of a component?

I'm getting a list of lists that each have one tuple. How do I get one list with several tuples?

How do I design the Storage class such that it accommodates any storage type and at the same time there is only one instance of each?

How do you allow only one instance of an inherited window to be open?

How do i add Points coordinates to a List of PointF of only one side of the rectangle?

How do I retrieve the contents of all the files in a directory in a list each one?

Given a list of links, how do I click and validate each one using C# or Protractor, with Selenium?

How do I list (ls) the content of a folder/directory recursively but to a depth of only one folder/directory?

How do I auto-layout a list of UIViews vertically, one on top of each other?

Spinner issues: How do I call list items within the spinner and use math for each one separately?

How to retrieve a list of record including only one child each?

I have a list that contains strings. How do I use .split() on each one (with forEach)?

How do I filter a list with date and time values by only extracting specific times each day?

How can I make only one instance of an application receive each AMQP topic message (consumer group behaviour)

How do I imitate having two list of items, while having only one list in css?

How do I create a new object for each class instance?

how do I write a program to print the length of each element in a list in this format 'One,3'

How can I issue a warning for only one instance of a module?