Question

I am using Sky Eagles nested set gem here.

I am only allowing the depth to be two deep, and I have a before save filter checking the depth, but I can't seem to get it to limit it in the view, so the customer doesn't even have the option to select it. I am using the view helper as described, shown here.

app/views/_form.html.erb

<%= form_for(@category) do |f| %>
  <p>
    <%= f.label(:name) %>
    <%= f.text_field :name %>
  </p>
  <p>
    <%= f.label(:parent_id) %>
    <%= f.select :parent_id, nested_set_options(Category.roots.to_a, @category) {|i, level| "#{'-' * level} #{i.name}" } %>
  </p>
  <p>
    <%= f.submit("Submit") %>
  </p>
<% end %>

The line i'm mainly interested in is this

<%= f.select :parent_id, nested_set_options(Category.roots.to_a, @category) {|i, level| "#{'-' * level} #{i.name}" } %>

Right now it's always returning all levels of the nested set, but I only want root nodes to display, since I am only allowing two deep. I am stuck here, because when I look at the code here I only see an option to include root, which is true and I am ok with that. I have tried doing this to the line

<%= f.select :parent_id, nested_set_options(Category.roots.to_a, @category) {|i, level| "#{'-' * level} #{i.name}" if i.level < 1} %>

But all this does is blank out the options that have a level less then two, I need it to actually not return them at all.

So, how do I get the nested_set_options to return ONLY the roots, NOT any category that has depth? Thanks in advance

Était-ce utile?

La solution

I think you are using the wrong helper here. nested_set_options was written to actually get all the descendants, too. I would suggest just adding it by hand:

f.select :parent_id, Category.roots.collect {|c| [ c.name, c.id ] }
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top