2015-08-21 55 views
0

我是新來的鐵軌,我想知道是否有一個簡單的(或不簡單)的方式來縮進DPS的每個孩子?縮進深度第一次搜索(軌道上的紅寶石)的每個孩子?

我有一個叫做「評論」的模型has_many:comments和belongs_to:comment。在我看來,我已經實現了一個DPS顯示每個評論並評論每個評論,對此有何評論每個評論等

我的代碼如下所示:

<div class=feed> 
 
\t <% @comments.each do |comment| %> 
 
\t \t <% if comment.comment_id == nil # display all original comments %> 
 
\t \t \t 
 
\t \t \t <!-- subject --> 
 
\t \t \t <div class="subject"> 
 
\t \t \t \t <%= comment.subject %>: 
 
\t \t \t </div> 
 

 
\t \t \t <!-- create array of replies --> 
 
\t \t \t <% replies = Array.new %> \t 
 
\t \t \t <% replies.push(comment) %> 
 
\t \t \t <% while replies.any? %> 
 

 
\t \t \t \t <% reply = replies[0] %> 
 
\t \t \t \t <% replies.delete_at(0) %> 
 
\t \t \t \t 
 

 
\t \t \t \t <!--- name --> 
 
\t \t \t \t <div class="comment"> 
 
\t \t \t \t \t <%= User.find(reply.user_id).name %> 
 
\t \t \t \t <!-- comment --> 
 
\t \t \t \t \t <%= reply.body %> 
 
\t \t \t \t \t <% if user_signed_in? %> 
 
\t \t \t \t \t \t <%= link_to "reply", new_comment_comment_path(reply.id) %> \t 
 
\t \t \t \t \t <% end %> 
 
\t \t \t \t </div> 
 

 
\t \t \t \t <% reply.comments.each do |further_replies| %> 
 
\t \t \t \t \t <% replies.push(further_replies) %> 
 
\t \t \t \t <% end %> 
 
\t \t \t \t <br> 
 
\t \t \t <% end %> 
 
\t \t \t <br> 
 
\t \t <% end %> 
 
\t <% end %> 
 
</div>

我將每個評論推送到「回覆」並逐一訪問每個回覆。

是否有縮進每個孩子評論的好方法?

謝謝!

+0

你能畫一個線框來理解你的觀點嗎? –

+0

我正在描述reddit(和現在的臉書)上的縮進線程。 –

回答

0

您可以使用act_as_tree結構,在這種情況下,你的模式將類似於 -

class Comment 
    has_many :replies, class_name: 'Comment', foreign_key :comment_id 
    belongs_to :user 
end 

你的HTML代碼將在像

<div class=feed> 
    <% @comments.each do |comment| %> 
    <div class="subject"> 
     <%= comment.subject %>: 
    </div> 
    <% comment.replies.each do |reply| %> 
     <div class="comment"> 
     <%= reply.user..name %> 
     <%= reply.body %> 
     <% if user_signed_in? %> 
      <%= link_to "reply", new_comment_comment_path(reply.id) %>  
     <% end %> 
     </div> 
     <br> 
    <% end %> 
    <br> 
    <% end %> 
</div> 
+0

感謝您的快速響應!好主意啊。我實現了它(並在Comment模型中添加了一行「acts_as_tree order:'created_at'」。但是我仍然在「has_many:回覆,class_name:'Comment',foreign_key:comment_id」中收到語法錯誤.... –

0

我想出了一個不同的方法,這種情況下很簡單那效果很好。我在「CommentsController」中實現了一個遞歸深度優先搜索,該搜索返回了{comments => amount_to_indent}的散列,其中評論的順序是他們被訪問的順序。在通過散列迭代的視圖文件中,在適當的地方使用comment和amount_of_indent。

class CommentsController < ApplicationController 

def index 
    @comments = Comment.all 

    # depth first search 
    @visited = Hash.new 
    @comments.each do |comment| 
    if !comment.comment_id  # has not parent comment 
     indent = 0 
     comment_array = DFS(comment, @visited, indent) 
    end 
    end 
end 

def DFS(comment, visited, indent) 
    visited[comment] = indent  # add elements in order in which they are visited 
    comment.comments.each do |reply| 
     DFS(reply, visited, indent + 4) 
    end 
    visited 
end 

並在視圖文件:

<div class="feed"> 
 
\t <% @visited.each do |reply, indent| %> 
 
\t \t <!-- display subject of all parent comments --> 
 
\t \t <div class="subject"> 
 
\t \t \t <% if !reply.comment_id? %> 
 
\t \t \t \t <%= reply.subject %>: 
 
\t \t \t <% end %> 
 
\t \t </div> 
 

 
\t \t <!--- name and content --> 
 
\t \t <div class="comment"> 
 
\t \t \t <!-- preceed comment with indent --> 
 
\t \t \t <%= raw(('&nbsp') * indent) %> 
 
\t \t \t <%= User.find(reply.user_id).name + ":" + reply.body %> 
 

 
\t \t \t <% if user_signed_in? %> 
 
\t \t \t \t <%= link_to "reply", new_comment_comment_path(reply.id) %> \t 
 
\t \t \t \t <!--%= link_to 'delete', response, method: :delete, data: { confirm: 'Are you sure?' } %--> 
 
\t \t \t <% end %> 
 

 
\t \t </div> 
 
\t <% end %> 
 
</div>

如果有人跑進我也做了同樣的問題!