Question

I'm following the rails cast Tree Based Navigation.

I want to order the sub-tree alphabetically.

In my PagesController I have

@page_for_nav = Admin::Page.find_by_permalink!(params[:id])

which gets called by

<%= render 'layouts/sub_navigation', pages: @page_for_nav.root.descendants.arrange %>

which renders

<ul>
  <% pages.each do |page, children| %>
    <% if page.page_status == 'Public' %>
    <li>
      <%= link_to_unless_current page.name, "../#{page.permalink}" %>
      <%= render 'layouts/sub_navigation', pages: children if children.present? %>
    </li>
    <% end %>
  <% end %>
</ul> 

Doing this in my PagesController fails (no error given, it just doesn't order)

@page_for_nav = Admin::Page.order("name asc").find_by_permalink!(params[:id])

and doing this on the each which also fails (no error given, it just doesn't order)

<% pages.each.order("name asc") do |page, children| %>
Was it helpful?

Solution

You should pass :order => :name option to arrange method

<%= render 'layouts/sub_navigation', pages: @page_for_nav.root.descendants.arrange(:order => :name) %>

Read the doc: https://github.com/stefankroes/ancestry#arrangement

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top