var modalSelector = "#<%= dom_id @releasable, "notes" %>" var modal = "<%= j render("notes_modal", releasable: @releasable, notes: @notes) %>" // If the modal already exists, replace it. Otherwise, create it if ($(modalSelector).length > 0) { $(modalSelector).replaceWith(modal) } else { $('body').append(modal) } $(modalSelector).modal("toggle")