我是 Rails 新手,我想知道是否有一种简单(或不简单)的方法来缩进 DPS 的每个子项?
我有一个名为“Comment”的模型,它有_many :comments 和belongs_to :comment。在我看来,我已经实现了一个 DPS 来显示每条评论和对该评论的每条评论,以及对该评论的每条评论等。
我的代码如下所示:
<div class=feed>
<% @comments.each do |comment| %>
<% if comment.comment_id == nil # display all original comments %>
<!-- subject -->
<div class="subject">
<%= comment.subject %>:
</div>
<!-- create array of replies -->
<% replies = Array.new %>
<% replies.push(comment) %>
<% while replies.any? %>
<% reply = replies[0] %>
<% replies.delete_at(0) %>
<!--- name -->
<div class="comment">
<%= User.find(reply.user_id).name %>
<!-- comment -->
<%= reply.body %>
<% if user_signed_in? %>
<%= link_to "reply", new_comment_comment_path(reply.id) %>
<% end %>
</div>
<% reply.comments.each do |further_replies| %>
<% replies.push(further_replies) %>
<% end %>
<br>
<% end %>
<br>
<% end %>
<% end %>
</div>
我将每条评论推送到“回复”上,并逐条访问每条回复。
有没有缩进每个孩子评论的好方法?
谢谢!