6

I'm not an ActiveAdmin expert. I need to have a number of customizations on some index pages. As recommended in the documentation, I'm trying to maintain AA pages build and then customize them by using Javascript. I succeeded by putting my own Javascript file under app/assets/javascripts, and then appending

//=require my_javascript_file

to app/assets/javascripts/active_admin.js

The problem is that this way the Javascript code gets loaded for ANY index page of any model. Which is the best way to insert Javascript code only for a specific model?

Thanks Thomas

zeroquaranta
  • 392
  • 4
  • 16
  • 2
    Can you just add `javascript_include_tag "my_javascript_file"` on the relevant page template? I don't see what ActiveAdmin has to do with this, though i might be misunderstanding you. – Max Williams Jun 02 '14 at 15:20
  • @MaxWilliams you are correct if you need page specific js or css you have to include it in view – Sabyasachi Ghosh Jun 03 '14 at 05:13
  • The thing is I'm not using classic RoR views, I'm just using ActiveAdmin to build the pages, i.e. `app/admin/my_model.rb:` `ActiveAdmin.register MyModel do` [Arbre code] `end` Said this, I tried using the command `script :src => javascript_path('my_javascript_file.js'), :type => "text/javascript"` to include the Javascript file in the page, but with no luck – zeroquaranta Jun 03 '14 at 08:11

3 Answers3

7

For me worked following:

form do |f|
  text_node javascript_include_tag "path_to/my_javascript_file"
...
Alexandr
  • 1,704
  • 1
  • 14
  • 20
  • 1
    Note that path should be relative to `app/assets/javascripts` folder + add it to `assets.rb`. – Al17 Jun 11 '20 at 17:29
0

I found the following worked well in my case. I added the following to config/initializers/assets.rb:

sub_folder = 'page_assets'
specific_assets_path = Rails.root.join('app', 'assets', 'javascripts', sub_folder).to_path
Dir.glob("#{specific_assets_path}/*.{js,coffee}").entries.each { |file|
  asset = "#{sub_folder}/#{File.basename(file, File.extname(file))}.js"
  Rails.application.config.assets.precompile += [ asset ]
}

Then I put my js and coffee files in app/assets/javascript/page_assets e.g my_page.coffee

Now in my view I add:

<%=
  javascript_include_tag 'page_assets/my_page'
%>

My view is an html.erb partial, but the same should work for a page that is defined in the my_page.rb file.

My active_admin.js just requires the active_admin base, because I don't want the active_admin asset to include all my per-page scripts:

//= require active_admin/base

The advantage of this mechanism is that I don't need to remember to add to the precompile list when I add a new per-page script - it's done automatically. ActiveAdmin won't add my page_assets scripts to the active_admin.js main asset (because I haven't got require_tree . in active_admin.js)

Derek Knight
  • 225
  • 2
  • 9
0

Based on the @Alexandr's answer,

  1. Add below line under form do loop as:

    form do |f| text_node javascript_include_tag "path_to/my_javascript_file" end

  2. Add path_to/my_javascript_file in confg/initializers/assets.rb to precompile it.

  3. Add

    //= link path_to/my_javascript_file.js

    to assets/config/manifest.js

Piyush Chaudhary
  • 183
  • 2
  • 12