如何为祖先树视图呈现嵌套的ul列表 [英] How to render nested ul list for ancestry tree view
本文介绍了如何为祖先树视图呈现嵌套的ul列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用content_tag渲染下面所示的结构,其中集合是祖先对象。
I want to render a structure shown below using content_tag where the collection is the ancestry object.
<ul>
<li>
<a>Fruits</a>
<ul>
<li>
<a>Apple</a>
</li>
<li>
<a>Orange</a>
</li>
</ul>
</li>
<li>
<a>Colours</a>
</li>
</ul>
推荐答案
我相信这是答案,社区,请编辑
I believe it's the answer, community, please, edit and tweak this post if it's wrong.
创建这样的帮助方法
def nested_groups(groups)
content_tag(:ul) do
groups.map do |group, sub_groups|
content_tag(:li, group.name + nested_groups(sub_groups))
end.join.html_safe
end
end
然后,将祖先对象传递给视图中的方法:
then, pass the ancestry object to the method in the view:
<%= nested_groups(@groups.arrange) %>
它将以正确的方式显示ul-list。
it will render the ul-list the right way.
这篇关于如何为祖先树视图呈现嵌套的ul列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文