0

我正在使用祖先 gem,我想要做的总是检测到当前帖子的所有相关帖子。

所以我的Post has_ancestry.

related posts我看来,我有这个:

<% if @post.has_children? %>
    <p>
        <ul>
    <% @post.children.each do |child| %> 
            <li><%= link_to child.title, post_path(child)%></li>        
    <% end %>
        </ul>
    </p>
<% else %>
    <p>
        There are no related posts.
    </p>
<% end %>
</div>

虽然这种方法很好,但它只检查一个用例(即,如果这篇文章有一个孩子)。我想要发生的事情是当这个人点击孩子时,在那个显示页面上它也应该将此帖子显示为相关帖子。

实际上它将是父母,但我不想做if post.has_parents?, then if post.has_children?,if post.has_siblings?等。

理想情况下,我只想检测任何亲属,如果有亲属,我想遍历它们并以同质的方式显示它们。

我怎么做?

4

0 回答 0