Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Active Admin : triggering a modal window from a click on an action_item link

I have that action_item on my admin page:

action_item :only => :index do
    link_to I18n.t('admin.dem_ref_nvl_etb'), :action => 'whatever'
  end

I'd like to know how I could display a pop-up window by clicking that link above, pretty much just like batch_action does when you use it with a "form" option (I don't need such an action here, it's just a basic link).

Any hint ?

Thanks a million for reading and helping!

like image 490
Git Psuh Avatar asked Dec 15 '22 07:12

Git Psuh


1 Answers

Building on Hugues's answer, here's a more fleshed-out example that I managed to cobble together with my very meager javascript skills:

In app/assets/javascripts/active_admin.js:

//= require active_admin/base

$(document).on('ready page:load turbolinks:load', function() {
  $('a.lextest').click(function(e) {
    e.stopPropagation();  // prevent Rails UJS click event
    e.preventDefault();

    ActiveAdmin.modal_dialog("Send email to: ", {emails: 'text'}, function(inputs) {alert (inputs.emails)})
  })
})

Note that I don't use the default active_admin.js.coffee because I dislike coffeescript -- just a personal preference. This code adds an onClick event handler to all links with class lextest. Now you can create such a link with a link_to:

link_to('Modal', '#', class: 'lextest')

I cobbled this all together from the way batch_action is implemented.

like image 191
Lexelby Avatar answered Apr 21 '23 03:04

Lexelby