Active Admin - How to add a custom script tag before the </body> tag?

I would like to do some analytics in my active application with permission support. To do this, I need to insert the <script> and <noscript> code immediately before the </body> in my layout file. Unfortunately, I cannot do this because the application.html layout file seems inefficient, since ActiveAdmin creates its own layout files.

Is there a hook / place where I can insert custom html code?

+4
source share
3 answers

Well, the first thing you will need to clone the repository into your rails application is that you usually put it in the provider directory, but rails throws this annoying warning that it will invalidate the use of the supplier directory directory style. it doesn't really matter if you are going to work with version 3. *, so just do it in the vendor or lib directory in your rails application.

git clone git://github.com/gregbell/active_admin.git

Now change your Gemfile and load the gem from the directory you installed

gem 'activeadmin', :path => 'lib/activeadmin'

Now you have your own version of activeadmin, so no matter what you need to edit, you can do it directly from this directory, including changing the default layout in which it is included.

A few tips:

Despite the fact that this method allows you to more accurately configure the active administrator, you are fully aware that in order to update it to a newer version, you will need to do git pull ing and merge if necessary.

I used this method with the jquery-ui-rails plugin and with another gem, it works great, and in addition, you can contribute to the restoration of the gem if you add the hook you want to the gem itself. Good luck

---- Edit ----

As you pointed out in the comment, activeadmin does not work exactly as you would expect, but don't worry that this is still a simple fix. Under the hood, activeadmin uses something called arbre , which is created and maintained by the same developer.

https://github.com/gregbell/arbre , this is just a DOM library for ruby.

So you need to do the following:

Browse to this file inside the activeadmin directory, which you just cloned lib/active_admin/views/footer.rb

this is the activeadmin application footer, as you can see inside the build method, which you can insert inside something like add_scripts and add below

 def add_scripts script :src => 'http://yoursource.com' end 

I'm not quite sure how the arbre syntax flows, but its not hard to understand.

Good luck

+2
source

Tested with ActiveAdmin 0.6.0 and Rails 4.0.5.

You can also override the arbre view used by the active administrator to render the footer. In your active_admin initializer active_admin add:

 # config/initializers/active_admin.rb require 'admin/analytics_footer' ActiveAdmin.setup do |config| config.namespace :admin do |admin| config.view_factory.footer = Admin::AnalyticsFooter end end 

And define the view:

 # lib/admin/analytics_footer.rb module Admin class AnalyticsFooter < ActiveAdmin::Views::Footer def build super render('layouts/analytics') end end end 

And put your ga tracking code in app/views/layouts/_analytics.html.erb . After the server restarts, the fragment should appear inside the footer at the end of the page.

+6
source

This is what worked for me. I found it on the activeadmin website ( https://activeadmin.info/10-custom-pages.html ).

 # app/admin/calendar.rb ActiveAdmin.register_page "Calendar" do index do render partial: 'calendar' end end # app/views/admin/calendar/_calendar.html.erb <h1>Hello</h1> <script> </script> 
0
source

All Articles