Dan Tappin
Dan Tappin

Reputation: 3032

Adding a dynamic Active Admin utility dropdown menu

I am new to Active Admin and am trying to add a dynamic utility dropdown menu like this:

  config.namespace :admin do |admin|
    admin.build_menu do |menu|
      menu.add label: "Company", priority: 1 do |company|
          company.add label: "ABC Company", url: "/admin/?company=abc"
          company.add label: "XZY Company", url: "/admin/?company=xyz"
      end
    end
  end

I have a Company model with a name (ABC Company) and tenant_name (abc) so I want to loop through the Companies and dynamically generate this menu. I have played with a few guesses but I can't figure this out.

Upvotes: 1

Views: 720

Answers (1)

MilesStanfield
MilesStanfield

Reputation: 4639

Here's how you can dynamically create a menu of companies that redirect to their respective edit pages. Don't forget you need to restart your server to see these changes.

config.namespace :admin do |admin|
  admin.build_menu do |menu|
    menu.add label: "Companies", priority: 1 do |company|
      Company.all.each do |existing_company|
        company.add label: existing_company.name, url: "/admin/companies/#{existing_company.id}/edit"
      end
    end
  end
end

Upvotes: 2

Related Questions