Will
Will

Reputation: 4705

ActiveAdmin Nested Resource Index View

I've got MenuItem's nested under Menu using a belongs_to

This works great if i do not declare the index block (active admin works it out automagically) but if i declare my own block it shows all the MenuItems for all the menus.

How can I tell my block to respect the nested resource?

UPDATE - I think this is to do with https://github.com/nebirhos/activeadmin-sortable-tree rather than activeadmin core.

So i guess the question becomes: Is it possible to use sortable tree with nested resources?

ActiveAdmin.register MenuItem do
    config.filters = false
    config.paginate = false
    belongs_to :menu
    sortable tree: true
    permit_params :title, :url, :menu_id

    index as: :sortable do
        label "Title" do |menu_item|
            link_to menu_item.title, edit_admin_menu_menu_item_path( menu_item.menu, menu_item )
        end
        actions defaults: false do |menu_item|
            link_to "Delete", admin_menu_menu_item_path( menu_item.menu, menu_item ), method: "delete", confirm: "Are you sure?"
        end
    end

    form do |f|
        f.inputs "Details" do
            f.input :title
            f.input :url
            f.input :menu_id, :as => :hidden
        end

        f.actions
    end

end

Upvotes: 1

Views: 2298

Answers (1)

Charles Maresh
Charles Maresh

Reputation: 3363

The issue is due to ActiveAdmin Sortable Tree's method of finding roots in the hierarchy. By default the sortable tree finds all roots for the model specified, regardless of nesting. This behavior may be customized by providing a proc to the :roots_collection option:

ActiveAdmin.register MenuItem do
  belongs_to :menu
  sortable tree: true,
          # Only display the parent menu's roots
          roots_collection: proc { parent.menu_items.roots }
end

Answer copied from my post on ActiveAdmin Sortable Tree #30.

Upvotes: 1

Related Questions