我使用 gemancestry
来创建评论。
现在,我可以列出所有评论。
但我想将序列号推送到每条评论。
例如,如果有 3 条评论,则第一个评论标注 1,下一条标注标注 2,..
我不知道该怎么做?
显示.html.haml
- if notice
%p.alert.alert-success= notice
= nested_comments(@comment.subtree.arrange(:order => :created_at))
帮手
def nested_comments(comments)
if comments.respond_to? :map
comments.map do |comment, sub_comments|
render(comment) + content_tag(:div, nested_comments(sub_comments), :class => "nested_comments")
end.join.html_safe
end
end
each_with_index 不适用于递归
如果我有 4 条评论,我想为每条评论显示 0,1,2,3
但是 each_with_index 不能成功,因为它是一个递归调用。
comments.each_with_index.map do |(comment, sub_comments), i|
注释
=> {#<Comment id: 2, user_id: 1, ip: nil, content: "I'm id2 the second floor VIVOTEK Releases New Vers...", commentable_id: nil, commentable_type: nil, created_at: "2014-11-07 03:59:38", updated_at: "2014-11-07 06:56:12", ancestry: nil>=>
{#<Comment id: 4, user_id: 1, ip: nil, content: "lala", commentable_id: nil, commentable_type: nil, created_at: "2014-11-07 05:22:41", updated_at: "2014-11-07 05:22:41", ancestry: "2">=>
{#<Comment id: 5, user_id: 1, ip: nil, content: "son of 4", commentable_id: nil, commentable_type: nil, created_at: "2014-11-07 06:38:04", updated_at: "2014-11-07 06:38:04", ancestry: "2/4">=>
{},
#<Comment id: 6, user_id: 1, ip: nil, content: "dild last 252", commentable_id: nil, commentable_type: nil, created_at: "2014-11-07 06:52:15", updated_at: "2014-11-07 06:52:15", ancestry: "2/4">=>
{}}}}