Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Active Admin Navigation Links

How do you add your own custom links dynamically to the ActiveAdmin global navigation header other than registering the pages/models? For example, if I want a link that can direct users to my home page for instance.

like image 503
RubyFanatic Avatar asked Feb 19 '23 19:02

RubyFanatic


2 Answers

Seems like ActiveAdmin made it somewhat easier. I upgraded to version 0.6.2 and here is what you can do to add custom links anywhere in your navigation (the example below will add one custom menu item, and one custom dropdown):

In # config/initializers/active_admin.rb

ActiveAdmin.setup do |config|
  config.namespace :admin do |admin|
    admin.build_menu do |menu|
      menu.add :label => "My Custom Link", :url => "/", :priority => 0

      menu.add :label => "Pages" do |pages|
        pages.add :label => "Homepage", :url => "/admin/homepage"
        pages.add :label => "About Us", :url => "/admin/about-us"
        pages.add :label => "Facebook", :url => "http://www.facebook.com", :html_options => { :target => "_blank" }
      end
    end
  end
end

In case you already registered models with "Pages" as a parent menu (ie: in your app/admin/ folder, a model with menu :priority => 2, parent: 'Pages') it will keep those as well automatically!

like image 184
rorofromfrance Avatar answered Mar 04 '23 19:03

rorofromfrance


I managed to to this by adding the ActiveAdmin::MenuItem to the current AdminAdmin controller. For example,

ActiveAdmin.register User, :name_space => :example_namespace do
  controller do
    private
    def current_menu
      item = ActiveAdmin::MenuItem.new :label => "Link Name", :url => 'http://google.com'
      ActiveAdmin.application.namespaces[:example_namespace].menu.add(item)
      ActiveAdmin.application.namespaces[:example_namespace].menu
    end
  end
end

I basically created a new ActiveAdmin::MenuItem and add it to the current ActiveAdmin menu with the namespace example_namespace and return the menu in the end of the current_menu method. Note: current_menu is a method expected by ActiveAdmin so don't change the name of it. You can add as many items you like and each of these items will be converted to a link on your navigation header. Note this works for ActiveAdmin version > 0.4.3 so you might need to do your own digging if you want to do it for version <= 0.4.3.

like image 41
RubyFanatic Avatar answered Mar 04 '23 17:03

RubyFanatic