Note that there are some explanatory texts on larger screens.

plurals
  1. PORecursion tree on Ruby on Rails
    primarykey
    data
    text
    <p>Following is the iterative tree, done in html.erb, which reaches only two levels in a tree structure:</p> <pre><code> &lt;ul&gt; &lt;li&gt;&lt;%= root_template.id %&gt;&lt;/li&gt; &lt;ul&gt; &lt;% for template in root_template.children %&gt; &lt;li&gt;&lt;%= template.id %&gt;&lt;/li&gt; &lt;% if template.has_children? %&gt; &lt;ul&gt; &lt;% for template_child in template.children %&gt; &lt;li&gt;&lt;%= template_child.id %&gt;&lt;/li&gt; &lt;% end %&gt; &lt;/ul&gt; &lt;% end %&gt; &lt;% end %&gt; &lt;/ul&gt; &lt;/ul&gt; </code></pre> <p>Result: <img src="https://i.stack.imgur.com/qFx1a.png" alt="iterative"></p> <p>I wanted to move the code in the helper files and apply a recursion to reach all the levels:</p> <p>html.erb (so, setting the root from template):</p> <pre><code> &lt;% html = '' %&gt; &lt;ul&gt; &lt;li&gt;&lt;%= root_template.id %&gt;&lt;/li&gt; &lt;ul&gt; &lt;%= recursive_tree root_template, html %&gt; &lt;/ul&gt; &lt;/ul&gt; </code></pre> <p>Then helper method:</p> <pre><code> def recursive_tree(root, html) html &lt;&lt; '' if !root.has_children? html &lt;&lt; "&lt;li&gt;#{root.id}&lt;/li&gt;" return html.html_safe else for template_child in root.children html &lt;&lt; "&lt;ul&gt;#{recursive_tree(template_child, html)}&lt;/ul&gt;" end end return html.html_safe end </code></pre> <p>Result:<img src="https://i.stack.imgur.com/lqXic.png" alt="Wrong Recursive Result"></p> <p>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?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload