2

I saw this post

Ruby on Rails - Awesome nested set plugin

but I am wondering how to do the same thing without using node? I am also wondering what exactly this node is doing in the code itself.

In my categories view folder i have _category.html.erb and a _full_categores_list.html.erb.

The _category.html.erb has the following code which is the sae way as the link above.

<li>
  <%= category.name %>

 <% unless category.children.empty? %>
   <ul>
      <%= render category.children %>
   </ul>
 <% end %>
</li>

The _full_categories_list.html.erb has the following code.

<ul>
  <% YourModel.roots.each do |node| %>
    <%= render node %>
  <% end %>
</ul>

This code works perfectly fine. However, lets say hypothetically that I wanted to create duplicates of these files so instead of _full_categories_list.html.erb I was maybe making a _half_categories_list.html.erb which might do something a little different with the code.

If I use similar code like what i used above in the _full_categories_list.html.erb it will keep calling the _category.html.erb.

How can I show all the cats, sub cats, and sub sub cats by using _half_categories_list.html.erb and a file like _half_category.html.erb instead of _category.html.erb

The half category and full category are just names to indicate that I am doing something different in each file. I hope this makes sense. I want to basically duplicate the functionality of the code from the link above but use _half_category.html.erb instead of _category.html.erb because I'm trying to put different functionality in the _half_category.html.erb file.

Community
  • 1
  • 1
jim
  • 1,137
  • 2
  • 12
  • 22

1 Answers1

2

First: there's a simpler way to write _full_categories_list.html.erb using render, with the :partial and :collection options.

<ul>
  <%= render :partial => :category, :collection => YourModel.roots %>
</ul>

This is equivalent to the _full_categories_list.html.erb you wrote above.

roots is a named scope provided by awesome_nested_set. You can add more scopes to your models - for example a named scope called half_roots (see the link for information about how).

With this in mind, _half_categories_list.html.erb could be written as follows:

<ul>
  <%= render :partial => :half_category, :collection => YourModel.half_roots %>
</ul>

You can then use _half_category.html.erb to render the categories in that special way you need.

Ramon Araujo
  • 1,743
  • 22
  • 31
kikito
  • 51,734
  • 32
  • 149
  • 189