ruby on rails – ActiveAdmin – Custom Javascript only for specific resource, not for any page
ruby on rails – ActiveAdmin – Custom Javascript only for specific resource, not for any page
For 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.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 dont want the active_admin asset to include all my per-page scripts:
//= require active_admin/base
The advantage of this mechanism is that I dont need to remember to add to the precompile list when I add a new per-page script – its done automatically. ActiveAdmin wont add my page_assets scripts to the active_admin.js main asset (because I havent got require_tree . in active_admin.js)