jyoseph
jyoseph

Reputation: 5455

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?

Upvotes: 0

Views: 1030

Answers (2)

Mike Andrianov
Mike Andrianov

Reputation: 3095

It's not quite a good way do this via unshift method. Use :include_blank => true instead.

<%= f.select :parent_id, nested_set_options(Page){|i| "#{'-' * i.level} #{i.name}" }, {:include_blank => true}, {:class => "form-control"} %>

Upvotes: 1

Adam Lassek
Adam Lassek

Reputation: 35515

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.

Upvotes: 2

Related Questions