Skip to content

Instantly share code, notes, and snippets.

@bondarolik
Created July 25, 2019 14:30
Show Gist options
  • Save bondarolik/465a00b395e0148fc8fa9588067d9866 to your computer and use it in GitHub Desktop.
Save bondarolik/465a00b395e0148fc8fa9588067d9866 to your computer and use it in GitHub Desktop.
Fire modal window and load contents from url
# VIEW where we have a link to fire modal. F.e., index.html.haml
# ...
= link_to "Edit form from modal", "#", title: "Title for modal window", data: { toggle: "modal", "load-url": edit_item(item), target: "#modalEditItem" }, class: "btn btn-sm btn-default"
# In the same view or corresponding JS asset
= content_for :scripts do
:javascript
$(document).ready(function(){
$('#modalEditItem').on('show.bs.modal', function (e) {
// load form (or other content) from remote url
// to a .modal-body
var loadurl = $(e.relatedTarget).data('load-url');
// Change modal title
var title = $(e.relatedTarget).prop('title');
// cnahge modal contents
$(this).find('.modal-body').load(loadurl);
$(this).find('.modal-title').html(title);
});
})
# Controller
def edit
render layout: false
end
# edit.html.haml
= render "items/modal_form"
# _modal_form.html.haml
= form_for @resource, method: :put, remote: true do |f|
.row
.col-sm-4
.form-group
= f.label :quantity, class: ""
= f.text_field :quantity, placeholder: Item.human_attribute_name(:quantity), class: "form-control"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment