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 = "<ul>" for category in categories if category.parent_id == 0 ret += "<li>" ret += link_to category.name ret += find_all_subcategories(category) ret += "</li>" end end ret += "</ul>" end def find_all_subcategories(category) if category.children.size > 0 ret = ''<ul>'' category.children.each { |subcat| if subcat.children.size > 0 ret += ''<li>'' ret += link_to h(subcat.name), :action => ''edit'', :id => subcat ret += find_all_subcategories(subcat) ret += ''</li>'' else ret += ''<li>'' ret += link_to h(subcat.name), :action => ''edit'', :id => subcat ret += ''</li>'' end } ret += ''</ul>'' 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. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
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 = "<ul>" for category in categories if category.parent_id == 0 ret += "<li>" ret += link_to category.name ret += find_all_subcategories(category) ret += "</li>" end end ret += "</ul>" end def find_all_subcategories(category) if category.children.size > 0 ret = ''<ul>'' category.children.each { |subcat| if subcat.children.size > 0 ret += ''<li>'' ret += link_to h(subcat.name), :action => ''edit'', :id => subcat ret += find_all_subcategories(subcat) ret += ''</li>'' else ret += ''<li>'' ret += link_to h(subcat.name), :action => ''edit'', :id => subcat ret += ''</li>'' end } ret += ''</ul>'' 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 can add <%= display_categories(@categories) %> in my layout ), I''m just confused as how to create under the correct parent. Thanks for any help... Very much appreciated. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Sorry, I must have accidently posted this twice. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
um ... i don''t fully get what your problem is. instead of listing the categories in a HTML List, put that list in a select dropdown box, set the name to "category_id", and the value of the options to the categories id values. then when you submit the form and "create" gets called, params[:news][:category_id] will contain the selected news category. save the news, and you''re done. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---