Gustavo Chaín
Gustavo Chaín

Reputation: 943

Add custom items at header menu in ActiveAdmin

I want to add a link like link_to ("Edit yout profile", edit_user (current_user)) at header in ActiveAdmin. Is that possible ?!

Upvotes: 17

Views: 17666

Answers (6)

Allison
Allison

Reputation: 2326

The correct way to do this (for a number of years) has been to create an Action Item; this is documented under Custom Actions: https://activeadmin.info/8-custom-actions.html

Note that to get it to render as a button with the same styling as the built-in action buttons, the contents of the action item block should contain a hyperlink (even if it doesn't go anywhere). There may be other ways to do this but I haven't bothered to dig into the source code for it.

Example

enter image description here

action_item :copy_parameters, only: :show do
  a(
    class:                 'clipboard-btn',
    'data-clipboard-text': JSON.pretty_generate(resource.parameters)
  ) { '📋  Copy parameters' }
end

action_item :go_to_some_path do
  link_to 'Go to the place!', admin_fun_stuff_path
end

Upvotes: 0

inye
inye

Reputation: 1796

To me the @kristinalim answer almost work but his custom_path do not work, this give me a error. The next code work for me (I have to define the routes)

routes = Rails.application.routes.url_helpers
config.namespace :admin do |admin|
    admin.build_menu do |menu|
        menu.add :label => 'Custom Menu' do |submenu|
            submenu.add label: 'Users', url: routes.admin_users_path
        end
    end
end

Upvotes: 0

kristinalim
kristinalim

Reputation: 3459

Recent versions of ActiveAdmin allow you to do this in your active_admin.rb initializer:

config.namespace :admin do |admin|
  admin.build_menu do |menu|
    menu.add :label => 'Custom Menu' do |submenu|
      submenu.add :label => 'Custom Link', :url => custom_path
    end
  end
end

Upvotes: 18

Alex Matchneer
Alex Matchneer

Reputation: 3309

If you're using a later version of ActiveAdmin that has the capability for custom pages, you can do the following:

 ActiveAdmin.register_page "Store Admin" do
   controller do
     define_method(:index) do
       redirect_to "/store/admin"
     end
   end
 end

This overrides the index action of the PageController which normally just renders the page, but you can instead have it redirect to wherever you want to go, such as edit_user_path

Upvotes: 7

rayban
rayban

Reputation: 1194

For recent versions of active admin, there are two solutions, both kind of unfortunate.

Any of your Resources with menu false can be set to menu label: "Google", url: "http://www.google.com". This is unfortunate because it is highlighted as selected if you are on the resource from which you set it.

The other solution is to over-write the ActiveAdmin::Views::Header as above, but updated.

module ActiveAdmin
  module Views
    class Header < Component

      def build_global_navigation
        item = ActiveAdmin::MenuItem.new(label: "google", url: "http://www.google.com")
        @menu.add item
        insert_tag view_factory.global_navigation, @menu, :class => 'header-item'
      end
    end
  end
end

This doesn't work exactly right, as you can't set things like parent: "Developer" for the menu item... Anyway, I may make a fork so you can add items in the initializer to a particular namespace...Did anyone open an issue for this? I didn't see one.

Update: I think this is the cleanest way to implement this (without contributing to active admin).

ActiveAdmin.register_page "Queue" do
  menu parent: "Developer", url: '/admin/resque'
end

Upvotes: 12

Siwei
Siwei

Reputation: 21569

according to the post that @phoet mentioned, (https://stackoverflow.com/a/7218598/445908) try this code:

module ActiveAdmin
  module Views
    class HeaderRenderer
      def to_html
        title + global_navigation + profile_link + utility_navigation
      end

      def profile_link
        link_to ("Edit yout profile", edit_user (current_user))
      end
    end
  end
end

Upvotes: -2

Related Questions