rails_admin arbitrary open modal forms
Asked Answered
H

3

6

Hi I'm using Rails Admin and I need to open arbitrary modal forms on certain models. To make it simple I would like to have a link which once clicked opens a modal form. I though it was just a matter of what class and "data-*" attribute is set on the link that triggers the modal form but looks like it's more complicated. How do I achieve this? I'm been browsing partial views and javascript in the gem to try to understand how this feature is achieved for example on one to many relations but the code is a bit beyond my knowledge and I can't get this done. Thanks

Hollis answered 4/3, 2013 at 22:47 Comment(0)
C
3

Although the Rails Admin remoteForm widget was built to work within a form, you can use it elsewhere like this:

In your view, inside a div, insert the button with the RA internal link in the data-link attribute, for example:

<div id="new-payment">
  <a href="#" data-link="<%= new_path(:payment, associations: {client: client.id}, modal: true) %>" class="create btn btn-info">
    New Payment
  </a>
</div>

Note that the link has the modal=true attribute. The outer div #new-payment is the DOM object where the widget will be applied to.

Another important hint is that the button must have the create class in case of new register, or update when updating an existing register.

Now you can call the remoteForm widget in your javascript (i.e. /app/assets/javascripts/rails_admin/custom/ui.js):

$(document).on('rails_admin.dom_ready', function() {
  $('#new-payment').remoteForm({
    success: function(data, status, xhr) { 
      $.pjax.reload({container:"[data-pjax-container]"}); 
    }
  });
});

In this case i'm taking advantage of pjax, and setting a 'success' callback, that is called after the payment is created, to reload the page's contents.

Collyer answered 14/1, 2016 at 16:28 Comment(0)
S
1

I was also trying to figure out the solution for this, my approach was to copy the functions called in the rails admin to my code base. Its bad approach but was working.

https://github.com/sferik/rails_admin/blob/master/app/assets/javascripts/rails_admin/ra.remote-form.js

create class is binded to action of opening the model, but it didn't work. So i took the _bindModalOpening, _bindFormEvents and _getModal functions into my code base.

Seller answered 4/8, 2014 at 8:27 Comment(0)
M
0

I don't think you can accomplish this through configuration. You will need to extend an existing action.

Muhammadan answered 22/3, 2013 at 0:42 Comment(1)
It's fine to me create a new custom action. The point is how do i link this action to be opened using the modal ui available in rails_admin?Hollis

© 2022 - 2024 — McMap. All rights reserved.