SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Thread: acts_as_tree

  1. #1
    SitePoint Member
    Join Date
    Dec 2004
    Location
    Syria
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy acts_as_tree

    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 explains 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:

    Code:
    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:

    Code:
    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:
    Code:
    class Category < ActiveRecord::Base
    acts_as_tree :order => "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):

    Code:
    <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.

  2. #2
    SitePoint Enthusiast
    Join Date
    Feb 2006
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    have you defined @categories anywhere

    You should have something like @categories = Category.find :all
    in your controller -> action or even in the view itself (ugly)

    at the moment @categories has never been defined

  3. #3
    SitePoint Member
    Join Date
    Dec 2004
    Location
    Syria
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Yes, I have @categories = Category.find(:all) defined in the controller:

    def display
    @categories = Category.find(:all)
    end

    view: display.rhtml
    <ul>
    <% for category in @categories %>
    <li><%= category.title%>
    </li>
    <% end %>
    </ul>

    The error message:
    NoMethodError in Admin/categories#display

    and it points to the line: <% for category in @categories %>

    Thanks

  4. #4
    SitePoint Member
    Join Date
    Jun 2005
    Location
    Eastbourne, UK
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you replace your view temporarily with

    Code:
    debug(@categories)
    what is the output?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •