acts_as_tree

Discussion in 'Ruby' started by AN@S, Jul 20, 2006.

  1. AN@S

    AN@S Guest

    Hello,
    I'm trying to create categories that have their own subcategories, and
    each subcategory may have its own subcategories, .. and so on.

    After some reading, I found out that the best way for creating such a
    thing is to use acts_as_tree, but I didn't find any tutorial or article
    that explain this in a clear way. After a lot of work I was finally
    able to create categories and subcategories using the following code
    explained in (Agile Web Development With Rails) book:

    ----------------------------------------------------------------
    root = Category.create:)name => "Books")
    fiction = root.children.create:)name => "Fiction")
    non_fiction = root.children.create:)name => "Non Fiction")
    non_fiction.children.create:)name => "Computers")
    non_fiction.children.create:)name => "Science")
    non_fiction.children.create:)name => "Art History")
    fiction.children.create:)name => "Mystery")
    fiction.children.create:)name => "Romance")
    fiction.children.create:)name => "Science Fiction")
    ------------------------------------------------------------------

    And here is the database scheme:

    ------------------------------------------------------------
    create table categories (
    id int not null auto_increment,
    name varchar(100) not null,
    parent_id int,
    constraint fk_category foreign key (parent_id) references
    categories(id),
    primary key (id)
    );
    ------------------------------------------------------------

    in the model:
    ------------------------------------------------------------
    class Category < ActiveRecord::Base
    acts_as_tree :eek:rder => "name"
    end
    ------------------------------------------------------

    OK, my problem now is how to view something! Any category name, or any
    subcategory or a list of categories, I tried a lot of things, like (in
    the view):

    ---------------------------------------------------------
    <ul>
    <% for category in @categories %>
    <li><%= link_to h(category.title), :action => 'edit', :id =>
    category %>
    <%= find_all_subcategories(category) %>
    </li>
    <% end %>
    </ul>
    ----------------------------------------------------------

    But I ALWAYS and whatever I try I got this error message:

    "You have a nil object when you didn't expect it!
    You might have expected an instance of Array.
    The error occured while evaluating nil.each

    Extracted source (around line #2): <% for category in @categories %> "


    What do you think the problem is?
    If you know some tutorial that fully explain how to use acts_as_tree to
    add and show categories that'll be great
    AN@S, Jul 20, 2006
    #1
    1. Advertising

  2. AN@S

    Mat Schaffer Guest

    Mat Schaffer, Jul 20, 2006
    #2
    1. Advertising

  3. AN@S

    James Britt Guest

    AN@S wrote:
    > Hello,
    > I'm trying to create categories that have their own subcategories, and
    > each subcategory may have its own subcategories, .. and so on.
    >
    > After some reading, I found out that the best way for creating such a
    > thing is to use acts_as_tree, but I didn't find any tutorial or article
    > that explain this in a clear way. After a lot of work I was finally
    > able to create categories and subcategories using the following code
    > explained in (Agile Web Development With Rails) book:
    >


    Please ask rails questions on the rails mailing list.

    http://lists.rubyonrails.org/mailman/listinfo/rails

    --
    James Britt

    http://www.ruby-doc.org - Ruby Help & Documentation
    http://www.artima.com/rubycs/ - The Journal By & For Rubyists
    http://www.rubystuff.com - The Ruby Store for Ruby Stuff
    http://www.jamesbritt.com - Playing with Better Toys
    http://www.30secondrule.com - Building Better Tools
    James Britt, Jul 20, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rainer Jung
    Replies:
    0
    Views:
    220
    Rainer Jung
    Jun 27, 2006
Loading...

Share This Page