Creating subcategories with acts_as_tree

I’ve been working on my first real RoR project and seemingly everything
is going well except I’m at a loss right now trying to figure out how to
create subcategories.

This is what I have so far:

Database:
Category Table
id
name
parent_id

News Table
id
title
content
category_id

Models:
CATEGORY
acts_as_tree
has_many :news

NEWS
belongs_to category

In my Application Helper I’ve set up the following:

def display_categories(categories)
ret = “


    for category in categories
    if category.parent_id == 0
    ret += “

  • ret += link_to category.name
    ret += find_all_subcategories(category)
    ret += “

  • end
    end
    ret += “

end

def find_all_subcategories(category)
if category.children.size > 0
ret = ‘


    category.children.each { |subcat|
    if subcat.children.size > 0
    ret += ‘

  • ret += link_to h(subcat.name), :action => ‘edit’, :id => subcat
    ret += find_all_subcategories(subcat)
    ret += ‘

  • else
    ret += ‘

  • ret += link_to h(subcat.name), :action => ‘edit’, :id => subcat
    ret += ‘

  • end
    }
    ret += ‘

end
end

In my controller I have set up create for news:
def create
@news = News.new(params[:news])
if @news.save
flash[:notice] = ‘News was successfully created.’
redirect_to :action => ‘list’
else
render :action => ‘new’
end
end

But I’m not sure what to add to this and what to put in my layout to be
able to select what category/subcategory when creating a news item, and
how to create subcategories with-in the parent. If I manually go into
the database and change the id numbers it will display properly, I’m
just confused as how to create.

Thanks for any help… Very much appreciated.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs