I am trying to convert a nested hash tree to nested HTML list. So far I've created Post and Tag model and implemented hierarchy to the Tag model using Closure Tree .
Following is the helper method I've found from another post to make a recursive method to render hash to a nested set of list:
def hash_list_tag(hash)
html = content_tag(:ul) {
ul_contents = ""
ul_contents << content_tag(:li, hash[:parent])
hash[:children].each do |child|
ul_contents << hash_list_tag(child)
end
ul_contents.html_safe
}.html_safe
end
I just inserted this code to my helper section (application_helper.rb) without changing anything.
Afterwards, I embedded the following inside the view page(index.html.erb) to render a hash to a nested HTML list:
<div>
<% hash_list_tag Tag.hash_tree do |tag| %>
<%= link_to tag.name, tag_path(tag.name) %>
<% end %>
</div>
However, I received this error:
ActionView::Template::Error (undefined method `each' for nil:NilClass):
1:
2:
3: <div>
4: <% hash_list_tag Tag.hash_tree do |tag| %>
5: <%= link_to tag.name, tag_path(tag.name) %>
6: <% end %>
7: </div>
app/helpers/application_helper.rb:14:in `block in hash_list_tag'
app/helpers/application_helper.rb:11:in `hash_list_tag'
app/views/posts/index.html.erb:4:in `_app_views_posts_index_html_erb__1316616690179183751_70207605533880'