开发者

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 %>
<开发者_如何学Python/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


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 %>


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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜