Search code examples
ruby-on-railsrubyruby-on-rails-plugins

awesome_nested_set How to not select a parent?


I just got awesome_nested_set put in place and all is working well. I converted over from acts_as_tree using Category.rebuild! listed on the github link.

Problem is, I do not have the option to create a node on the top level with no parent (e.g. - There is no blank <option> in the form select). This is the select_tag I am using:

<%= select_tag 'parent_id', options_for_select(nested_set_options(Page) {|i| "#{'..' * i.level} #{i.name}" } ) %>

I'm a RoR newb so I'm unsure of how to make it so I can create a page on the top level. Can someone point me in the right direction?


Solution

  • In order to create a record at the root level, simply leave parent_id nil. You will need to add an option for that:

    <%= select_tag 'parent_id', options_for_select(
          nested_set_options(Page) {|i| "#{'..' * i.level} #{i.name}" }.unshift(["No Parent", nil])
        ) %>
    

    It's probably time to move this into a helper.