如何仅列出每个评论的一个实例?

时间:2018-12-01 13:46:31

标签: ruby-on-rails ruby

我有一个管理仪表盘,该仪表盘可接收所有博客文章中的评论并列出它们,以便我更轻松地进行管理。问题在于它显示了第二个评论实例作为答复,这使其显得有些混乱。如何阻止它两次列出答复?当他们在帖子的视图中呈现时,这不是问题。

我如何在 user.show视图中调用列表:

<%= render(partial: 'comments/comment', collection: @comments) %>

users_controller(显示方法): @comments = Comment.all

_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>

这是它的样子:

enter image description here

编辑: 在他们各自的帖子视图中渲染它们时,我没有这个问题-仅当在用户的show视图中渲染它们时。

我正在通过多态关系处理我的评论:

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

评论模型:

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

2 个答案:

答案 0 :(得分:0)

经过一些测试,通过在users_controller中使用它,我只能列出我的评论一次:

@comments   = Comment.where(commentable_type: "Post")

而不是使用它:

@comments   = Comment.all

这不仅列出了属于某个帖子的评论,而且还允许将属于另一个评论(作为回复)的评论列在其下方。

答案 1 :(得分:-1)

如果您不希望子注释看起来像是顶级注释,则需要在呈现部分内容之前将它们从@comments集合中删除。您仍然使用

呈现子注释。
obj

因此也无需将其呈现为顶级注释。

代替

const o = { foo: 'bar' };

const a = [{ bar: 'z' }, o];
const b = [{ bar: 'z' }, { ...o }];

console.log(o === a[1]); // true
console.log(o === b[1]); // false

尝试

<iframe width="640" height="360" src="" data-src="https://www.youtube.com/embed/PgcokT0AWHo" frameborder="0" allowfullscreen></iframe>

<script>
function init() {
    var vidDefer = document.getElementsByTagName('iframe');
    for (var i=0; i<vidDefer.length; i++) {
        if(vidDefer[i].getAttribute('data-src')) {
            vidDefer[i].setAttribute('src',vidDefer[i].getAttribute('data-src'));
        } 
    } 
}
window.onload = init;
</script> 

最好在模型中放入类似的内容,所以也许:

<%= render partial: 'comments/comment', collection: comment.comments %>

在您的控制器中:

@comments   = Comment.all

编辑:这假定注释具有comment_id以建立其父注释