Following is the iterative tree, done in html.erb, which reaches only two levels in a tree structure:
<ul>
<li><%= root_template.id %></li>
<ul>
<% for template in root_template.children %>
<li><%= template.id %></li>
<% if template.has_children? %>
<ul>
<% for template_child in template.children %>
<li><%= template_child.id %></li>
<% end %>
</ul>
<% end %>
<% end %>
</ul>
</ul>
Result:

I wanted to move the code in the helper files and apply a recursion to reach all the levels:
html.erb (so, setting the root from template):
<% html = '' %>
<ul>
<li><%= root_template.id %></li>
<ul>
<%= recursive_tree root_template, html %>
</ul>
</ul>
Then helper method:
def recursive_tree(root, html)
html << ''
if !root.has_children?
html << "<li>#{root.id}</li>"
return html.html_safe
else
for template_child in root.children
html << "<ul>#{recursive_tree(template_child, html)}</ul>"
end
end
return html.html_safe
end
Result:
I already spent a day to figure out how to send proper html from helper to template, now couldn't figure out what is the issue with this recursion and the solution even I used a debugger. Is there any opinion?