From afa4a2c98576b406a80b2a7ea6c955c9a39e4f75 Mon Sep 17 00:00:00 2001 From: evazion Date: Fri, 10 Jan 2020 19:16:27 -0600 Subject: [PATCH] js: fixup issues with @rails/ujs. Fix a couple regressions caused by the migration from jquery-ujs to @rails/ujs in 9f4ac4c96: * Add authenticity tokens to all remote forms. By default, Rails doesn't doesn't include authenticity tokens in remote forms because it can cause problems with fragment caching. This was fine with jquery-ujs because it would insert the authenticity token when the remote form was submitted, but apparently @rails/ujs doesn't do this. This broke certain remote forms nested inside of jquery UI dialogs. * Fix dialogs to trigger remote form submissions through @rails/ujs instead of through jquery. This fixes a problem where remote forms that returned a javascript response displayed the response as plaintext instead of executing the returned javascript. --- app/helpers/application_helper.rb | 1 + app/javascript/src/javascripts/artist_commentaries.js | 4 +++- app/javascript/src/javascripts/posts.js.erb | 4 +++- app/javascript/src/javascripts/utility.js | 4 +++- 4 files changed, 10 insertions(+), 3 deletions(-) diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 3a92f76e6..5639aad70 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -199,6 +199,7 @@ module ApplicationHelper def edit_form_for(model, **options, &block) options[:html] = { autocomplete: "off", **options[:html].to_h } + options[:authenticity_token] = true if options[:remote] == true simple_form_for(model, **options, &block) end diff --git a/app/javascript/src/javascripts/artist_commentaries.js b/app/javascript/src/javascripts/artist_commentaries.js index 0f75a7680..6842f687b 100644 --- a/app/javascript/src/javascripts/artist_commentaries.js +++ b/app/javascript/src/javascripts/artist_commentaries.js @@ -1,4 +1,5 @@ import Utility from "./utility"; +import Rails from "@rails/ujs"; let ArtistCommentary = {}; @@ -34,7 +35,8 @@ ArtistCommentary.initialize_edit_commentary_dialog = function() { width: 700, buttons: { "Submit": function() { - $("#add-commentary-dialog #edit-commentary").submit(); + let form = $("#add-commentary-dialog #edit-commentary").get(0); + Rails.fire(form, "submit"); $(this).dialog("close"); }, "Cancel": function() { diff --git a/app/javascript/src/javascripts/posts.js.erb b/app/javascript/src/javascripts/posts.js.erb index e252d29fc..bbce80f17 100644 --- a/app/javascript/src/javascripts/posts.js.erb +++ b/app/javascript/src/javascripts/posts.js.erb @@ -3,6 +3,7 @@ import Utility from './utility' import Hammer from 'hammerjs' import Cookie from './cookie' import Note from './notes' +import Rails from '@rails/ujs' let Post = {}; @@ -556,7 +557,8 @@ Post.initialize_saved_searches = function() { autoOpen: false, buttons: { "Submit": function() { - $("#save-search-dialog form").submit(); + let form = $("#save-search-dialog form").get(0); + Rails.fire(form, "submit"); $(this).dialog("close"); }, "Cancel": function() { diff --git a/app/javascript/src/javascripts/utility.js b/app/javascript/src/javascripts/utility.js index 9efff3d45..78811385a 100644 --- a/app/javascript/src/javascripts/utility.js +++ b/app/javascript/src/javascripts/utility.js @@ -1,4 +1,5 @@ import CurrentUser from "./current_user"; +import Rails from '@rails/ujs'; let Utility = {}; @@ -54,7 +55,8 @@ Utility.dialog = function(title, html) { }, buttons: { "Submit": function() { - $dialog.find("form").submit(); + let form = $dialog.find("form").get(0); + Rails.fire(form, "submit"); }, "Cancel": function() { $dialog.dialog("close");