Rails beginner
Rails beginner

Reputation: 14504

Rails ancestry treemenu help

I am trying to make a simpel treelistmenu with ancestry.

Here is my code in view and it is not working:

<ul>
<% for cat_opg in CatOpg.roots %>
 <li> <%= cat_opg.navn %><li>
 <% for cat_opg in CatOpg.children %>
<li> <%= cat_opg.navn %><li>
</ul>
<% end %>
</ul>
<% end %>

And my controller: def liste @cat_opg = CatOpg.find(:all) end

I want to make this simpel tree menu as:

Root

-children

Root

-children

I dont know what i am doing wrong. PS: I am a rails beginner

Upvotes: 0

Views: 789

Answers (2)

Rails beginner
Rails beginner

Reputation: 14504

Alex thank you for your answer.

Now it works

Controller: @cat_opg = CatOpg

And the view:

<ul>
  <% @cat_opg.roots.each do |cat_opg_root| %>
    <li> <%= cat_opg_root.navn %></li>
      <% unless cat_opg_root.children.empty? %>
       <ul>
         <% cat_opg_root.children.each do |cat_opg_child| %>
           <li> <%= cat_opg_child.navn %></li>
         <% end %>
       </ul>
     <% end %>
  <% end %>
</ul>

Upvotes: 0

Oleksandr Slynko
Oleksandr Slynko

Reputation: 787

First of all, you are getting to model in view, not to local variable.
Second, you're rewriting variable.

It should be something like this:

<ul>
<% cat_opg.roots.each do |cat_opg_root| %>
 <li> <%= cat_opg_root.navn %><li>
 <% cat_opg_root.children each do |cat_opg_child| %>
  <li> <%= cat_opg_child.navn %><li>
  </ul>
 <% end %>
 </ul>
<% end %>

Upvotes: 1

Related Questions