Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Customize the active admin layout
# config/initializers/active_admin.rb
# ...rest of the initializer here...
module AdminPageLayoutOverride
def build(*args)
# you can move the call to super at the end, if you wish
# to insert things at the begining of the page
super
# this will be added at the end of <body>
within @body do
render partial: '...'
end
# this will be added at the end of <head>
within @head do
text_node(javascript_include_tag('...))
end
end
end
ActiveAdmin::Views::Pages::Base.send :prepend, AdminPageLayoutOverride
@coderliu

This comment has been minimized.

Copy link

@coderliu coderliu commented Jun 28, 2018

You are life saver! Thanks!

@armchairdj

This comment has been minimized.

Copy link

@armchairdj armchairdj commented Aug 17, 2018

Ditto - total lifesaver! However, On ActiveAdmin 1.2.1, this gives an error:

ActionView::Template::Error (Can't be in the context of nil. [, <!DOCTYPE html><html lang="en">

You'll have to prepend to #build_active_admin_head and #build_body instead of directly to #build.

In my use case, I wanted to prepend a partial BEFORE the ActiveAdmin masthead/page header, so I did this:

module AdminPageLayoutOverride
  def build_page(*args)
    within @body do
      render "layouts/global/environment_banner"
    end

    super
  end
end

ActiveAdmin::Views::Pages::Base.send :prepend, AdminPageLayoutOverride

Here's the relevant class we're patching:

https://github.com/activeadmin/activeadmin/blob/1-2-stable/lib/active_admin/views/pages/base.rb

If you're on another version of the gem, you can look at the version from that branch to figure out your exact patching strategy.

@benjaminwood

This comment has been minimized.

Copy link

@benjaminwood benjaminwood commented Feb 7, 2019

Thanks all! This was helpful.

In Active Admin 1.4.3, it's like this:

module AdminPageLayoutOverride
  def build_page(*args)
    within body(class: body_classes) do
      render partial: 'layouts/global/environment_banner'
    end

    super
  end
end

ActiveAdmin::Views::Pages::Base.send :prepend, AdminPageLayoutOverride

Reference:
https://github.com/activeadmin/activeadmin/blob/v1.4.3/lib/active_admin/views/pages/base.rb#L48

@brightbytes-dude

This comment has been minimized.

Copy link

@brightbytes-dude brightbytes-dude commented Jun 5, 2020

Is there a way to do this now in AA 2.7?

This approach gives me:

     ActionView::Template::Error:
       Can't be in the context of nil. [, <!DOCTYPE html><html lang="en">

... because @head is nil when this is evaluated. Same thing occurs with super at the end of the method too.

Actually, answered this myself based upon the source: just replace @head with head and @body with body, and you're off to the races.

@leenasn

This comment has been minimized.

Copy link

@leenasn leenasn commented Jun 24, 2020

module AdminPageLayoutOverride
  def build_active_admin_head
    super
    # this will be added at the end of <head>
    within head do
      text_node(javascript_pack_tag("application"))
    end
  end
end

ActiveAdmin::Views::Pages::Base.send :prepend, AdminPageLayoutOverride

This worked for me in AA 2.2. But I noticed that it adds two head tags. Any clue on how to fix it?

@brightbytes-dude

This comment has been minimized.

Copy link

@brightbytes-dude brightbytes-dude commented Jun 24, 2020

LOL - I just looked at the HTML in AA 2.7, and found the JS and CSS tags in the <body>. Only one <head> though.

It works correctly if you put the super at the end instead of the beginning:

module AdminPageLayoutOverride
  def build(*args)
    # This will be added at the beginning of <head>
    within head do
      # stuff here
    end

    # This will be added at the beginning of <body>
    # within body do

    # end

    super
  end
end
ActiveAdmin::Views::Pages::Base.prepend(AdminPageLayoutOverride)

That being said, I think the recommended way to do this now is via the # Register Stylesheets & Javascripts section of the active_admin.rb initializer.

If you don't have that section (I didn't), then assuming your codebase is in a repo, you can rerun the AA generator to get the updated initializer, skipping everything you can except the initializer generation, reverting everything except the initializer changes, and then using the diff to decide what to revert in the initializer. (There may be a better procedure than that, but that does work.)

@tobycox

This comment has been minimized.

Copy link

@tobycox tobycox commented Jul 14, 2020

Note that the above didn't work for me on AA 2.7, but this comment did: activeadmin/activeadmin#5201 (comment)

@leenasn

This comment has been minimized.

Copy link

@leenasn leenasn commented Jul 14, 2020

Note that the above didn't work for me on AA 2.7, but this comment did: activeadmin/activeadmin#5201 (comment)

That worked. Thanks @tobycox.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment