I'm not an ActiveAdmin expert. I need to have a number of customizations on some index pages. As remended 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
I'm not an ActiveAdmin expert. I need to have a number of customizations on some index pages. As remended 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
Share Improve this question asked Jun 2, 2014 at 15:08 zeroquarantazeroquaranta 4127 silver badges20 bronze badges 3-
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 Commented Jun 2, 2014 at 15:20 - @MaxWilliams you are correct if you need page specific js or css you have to include it in view – Sabyasachi Ghosh Commented Jun 3, 2014 at 5: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 mandscript :src => javascript_path('my_javascript_file.js'), :type => "text/javascript"
to include the Javascript file in the page, but with no luck – zeroquaranta Commented Jun 3, 2014 at 8:11
3 Answers
Reset to default 7For me worked following:
form do |f|
text_node javascript_include_tag "path_to/my_javascript_file"
...
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.prepile += [ 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 prepile 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)
Based on the @Alexandr's answer,
Add below line under form do loop as:
form do |f| text_node javascript_include_tag "path_to/my_javascript_file" end
Add
path_to/my_javascript_file
inconfg/initializers/assets.rb
to prepile it.Add
//= link path_to/my_javascript_file.js
to
assets/config/manifest.js