Re: acts as tree simple menu

Any idea how to get this tree style into a select menu that will have unlimited levels?

Re: acts as tree simple menu

Hello,

Newbie here

Re: acts as tree simple menu

Try renaming the "list" action to "index".

Also, looks like you're using zero as the root parent_id value. In that case the find by "nil" won't work, you'll need to either find by "0" or change your table content.

Railscasts - Free Ruby on Rails Screencasts

Re: acts as tree simple menu

Cool, Thanks ryanb... I assume it is possible to display it in a drop down?

Thanks again

Re: acts as tree simple menu

New2RubyOnRails wrote:

Cool, Thanks ryanb... I assume it is possible to display it in a drop down?

Yeah it's certainly possible, but the difficulty depends on what you want to do (have it sorted properly with indents is kind of hard). I recommend starting a separate thread on this if you need to do this.

Railscasts - Free Ruby on Rails Screencasts

Re: acts as tree simple menu

I get this error, any idea what I am doing wrong?

Showing app/views/reporter/_side_nav.rhtml where line #10 raised:

undefined method `children' for ReportGroup:Class

Extracted source (around line #10):

7:    </ul>
8:
9:     <li><%= link_to ReportGroup.name, {:controller => 'reporter', :action => "list_reports", :id => ReportGroup.id } %></li>
10:     <% if ReportGroup.children.empty? %>

db(report_groups)

'id', 'int(11)', 'NO', 'PRI', '', 'auto_increment'
'name', 'varchar(255)', 'YES', '', '', ''
'parent_id', 'int(11)', 'YES', '', '', ''
'position', 'int(11)', 'YES', '', '', ''

report_group.rb
class ReportGroup < ActiveRecord::Base
  acts_as_tree :order => "name"
end

reporter_controller.rb
class ReporterController < ApplicationController

  def index
    @page_title = 'Reporter Dashboard'
    @statistics = Stat.find(:all, :order => "name")
    @graphs = Graph.find(:all, :order => "name")
    @report_groups = ReportGroup.find:all
  end


views/layouts/reporter.rhtml
<% content_for :side_nav do %>
    <%# TODO: move reporter side_nav in here when it's considered complete %>
    <%= render :partial => 'reporter/side_nav' %>
<% end %>

<% session[:layout] = :reporter %>
<% inside_layout 'application' do %>
    <%= yield %>
<% end %>


views/reporter/_side_nav.rhtml
<p>
  Standard Reports

 
   <ul>
     <%= render :partial => 'report_group', :collection => ReportGroup.find_all_by_parent_id(nil) %>
   </ul>

    <li><%= link_to ReportGroup.name, {:controller => 'reporter', :action => "list_reports", :id => ReportGroup.id } %></li>
    <% if ReportGroup.children.empty? %>
      <span> No sub</span>
      <% else %>
        <ul><%= render :partial => 'report_group', :collection => ReportGroup.children %></ul>
        <% end %>
      <ul>
        <% ReportGroup.standard_reports.each do |sr| %>
          <% if rg.name.downcase == 'summary'%>
            <li><%= link_to sr.name, {:controller => 'reporter', :action => 'show', :id => sr.id }%></li>
          <% end %>
        <% end %>
      </ul>

  </ul>
</p>

Re: acts as tree simple menu

Hello evebody!
It's a good example.
But I can't create a new subcategory.That's what I have:

A piece of code from the schema:
create_table "regional_objects", :force => true do |t|
    t.string   "name"
    t.integer  "parent_id"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

My model :
class RegionalObject < ActiveRecord::Base
  acts_as_tree :order => "name"
#the rest of associations is deleted just for the example
end

My 'index' page:
<% if @regional_objects.empty? %>
  <h4> No Granting regions found</h4>
<% else %>
  <h4>Listing Granting regions</h4>
  <%= render :partial => 'regional_object', :collection => @regional_objects%>
 
  <%= will_paginate @regional_objects %>
<%end%>
<br />
<%= link_to 'New granting region', new_regional_object_path %>

My partial (regional_object):
<table>
  <td><%= h regional_object.name%></td>
  <td><%= link_to 'Sub-Regions', regional_object%></td>
  <td><%= link_to 'Edit', edit_regional_object_path(regional_object)%></td>
  <td><%= link_to 'Destroy', regional_object, :confirm => 'Are you sure?', :method => :delete%></td>
</table>

My 'show' page:
<p>
  <b>Name:</b>
  <%=h @regional_object.name %>
</p>
<%if @regional_object.children.empty?%>
  <h4>No sub regions found</h4>
<%else%>
  <%= render :partial => 'regional_object', :collection => @regional_object.children %>
<%end%>
<br/>
<%= link_to 'Add', new_regional_object_path%>|
<%= link_to 'Back', regional_objects_path %>

My 'new' page:
<% form_for @regional_object do |form| %>
  <%= form.error_messages %>
  <h1>Create a Sub-Region for <%=h @regional_object.parent.name%></h1>
  <p>
    <%= form.label :name, "Sub-Region" %><br />
    <%= form.text_field :name %>
  </p>
  <p>
    <%= submit_tag "Create" %>
  </p>

<% end %>
<%= link_to 'Back', @regional_object.parent %>


How to get parent_id in my Controller (edit, new, delete and update actions) in this case?
Should I change anything in 'routes.rb' ? I use the same technics but between models with relations as one-to-many( by the same way I can add one or many 'packagings' for a 'product' for example. But in this case I have:
map.resources :products, :has_many => :packagings

How to get work the acts_as_tree' plugin in my controller?

Last edited by Javix (2009-02-09 09:55:12)

Re: acts as tree simple menu

HI

every nice tutorial on acts_as_tree

am using acts_as_tree and yui to generate tree structure,
as per the DB is consern for acts_as_tree,the parent and child nodes are from one table..


i have two different table like


region table
-------------
pk     name       
1      hyderabad
2      chennai
3      bangalore


area tabel
id     name          fk
1      madhapur      1
2      hi-tech       1
3      kondapur      1
4      adayar        2
5      ECR road      2
6      anna nagar    2
7      whitefiled    3
8      MGM           3



so i need name in region table as root node and the respective fk names in area table as child nodes(i dont need subchild node )..



how can i use acts_as_tree for this table,are is there an alternative to do this

Re: acts as tree simple menu

This tutorial is excellent.

I'd like to extend Ryan's code to order the tree by two conditions. The first, would be for each level of the tree to display in alphabetical order. The second condition would be a "sticky" boolean field included in each category that would take priority over the alphabetical order condition.

So, the output for a life tree would look like this:

- Animal
-- Mammal (sticky field is true)
--- Human (sticky field is true)
--- Dolphin
--- Whale
-- Bird
-- Dinosaur
-- Fish
- Plant
-- Flower
-- Tree
--- Oak