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
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.
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.
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.
I don't think you can accomplish this through configuration. You will need to extend an existing action.
© 2022 - 2024 — McMap. All rights reserved.