-
-
Save sahidursuman/3899eb76443d474a95f8714ffdb84f31 to your computer and use it in GitHub Desktop.
Rails Helper to build Hierachical HTML List
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module TreeListHelper | |
# the collection need to be the root parents | |
def tree_list(collection) | |
content_tag(:ul) do | |
collection.each do |item| | |
if item.children.any? | |
concat( | |
content_tag(:li, id: item.id) do | |
concat(item.name) | |
concat(tree_list(item.children)) | |
end | |
) | |
else | |
concat(content_tag(:li, item.name, id: item.id)) | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment