views:

108

answers:

1

Hello,

I saw this post

http://stackoverflow.com/questions/2059920/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.

+1  A: 

First: there's a simpler way to write _full_categories_list.html.erb using render, with the :template 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.

egarcia