From ec754641c1e96cb78ec8c9afe191b62c9a2c010f Mon Sep 17 00:00:00 2001 From: Jose Galisteo Date: Thu, 5 Jul 2018 10:05:37 +0200 Subject: [PATCH 1/3] Make it works with CSRF token Rails 5.1 has the CSRF protection disabled by default but rails 5.2 enable it again. https://github.com/rails/rails/pull/29742 - Remove the fancy ajax delete. - Use jquery_ujs for the delete action (it must be included by the host app at the moment). - Add `csrf_meta_tags` on the layout. --- .../featurer_web/anchors_to_delete.js | 20 ------------------- .../javascripts/featurer_web/application.js | 2 +- .../featurer_web/features_controller.rb | 3 ++- .../layouts/featurer_web/application.html.erb | 1 + 4 files changed, 4 insertions(+), 22 deletions(-) delete mode 100644 app/assets/javascripts/featurer_web/anchors_to_delete.js diff --git a/app/assets/javascripts/featurer_web/anchors_to_delete.js b/app/assets/javascripts/featurer_web/anchors_to_delete.js deleted file mode 100644 index d2f979e..0000000 --- a/app/assets/javascripts/featurer_web/anchors_to_delete.js +++ /dev/null @@ -1,20 +0,0 @@ -$(document).ready(function() { - $("a[data-method=delete]").on('click', function(e) { - e.preventDefault(); - - if (confirm("Are you sure?") == true) { - theElement = $(this); - - $.ajax({ - url: $(this).attr("href"), - type: "DELETE", - success: function() { - row = theElement.closest("tr"); - row.fadeOut(400, function() { - row.remove(); - }); - } - }); - } - }); -}); diff --git a/app/assets/javascripts/featurer_web/application.js b/app/assets/javascripts/featurer_web/application.js index b362f49..4b9796e 100644 --- a/app/assets/javascripts/featurer_web/application.js +++ b/app/assets/javascripts/featurer_web/application.js @@ -10,5 +10,5 @@ // Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details // about supported directives. // - //= require_tree . +//= require jquery_ujs diff --git a/app/controllers/featurer_web/features_controller.rb b/app/controllers/featurer_web/features_controller.rb index f55a9da..df0a72a 100644 --- a/app/controllers/featurer_web/features_controller.rb +++ b/app/controllers/featurer_web/features_controller.rb @@ -23,7 +23,8 @@ def create def destroy Featurer.delete(params[:id]) - head 200 + flash[:info] = "Feature deleted" + redirect_to root_path end private diff --git a/app/views/layouts/featurer_web/application.html.erb b/app/views/layouts/featurer_web/application.html.erb index ee06b7d..6d52dcf 100644 --- a/app/views/layouts/featurer_web/application.html.erb +++ b/app/views/layouts/featurer_web/application.html.erb @@ -7,6 +7,7 @@ + <%= csrf_meta_tags %> <%= stylesheet_link_tag "https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css", media: "all" %> <%= stylesheet_link_tag "featurer_web/application", media: "all" %> From 1bd5ba63ed1769b60ea07fc0d9afac4547235872 Mon Sep 17 00:00:00 2001 From: Jose Galisteo Date: Thu, 5 Jul 2018 16:40:13 +0200 Subject: [PATCH 2/3] [Gemspec] Ensure it is loaded as utf-8 Fixes bundler/bundler#6220 --- featurer_web.gemspec | 1 + 1 file changed, 1 insertion(+) diff --git a/featurer_web.gemspec b/featurer_web.gemspec index 53c8957..cdc396a 100644 --- a/featurer_web.gemspec +++ b/featurer_web.gemspec @@ -1,3 +1,4 @@ +# -*- encoding: utf-8 -*- $LOAD_PATH.push File.expand_path('../lib', __FILE__) # Maintain your gem's version: From b2f2893f894f33c0b2bbb6c0a90e38fe7789a8d9 Mon Sep 17 00:00:00 2001 From: Jose Galisteo Date: Fri, 6 Jul 2018 09:17:55 +0200 Subject: [PATCH 3/3] Add jquery-rails as dependency --- app/assets/javascripts/featurer_web/application.js | 3 ++- app/views/layouts/featurer_web/application.html.erb | 1 - featurer_web.gemspec | 1 + lib/featurer_web/engine.rb | 1 + 4 files changed, 4 insertions(+), 2 deletions(-) diff --git a/app/assets/javascripts/featurer_web/application.js b/app/assets/javascripts/featurer_web/application.js index 4b9796e..646c5ab 100644 --- a/app/assets/javascripts/featurer_web/application.js +++ b/app/assets/javascripts/featurer_web/application.js @@ -10,5 +10,6 @@ // Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details // about supported directives. // -//= require_tree . +//= require jquery //= require jquery_ujs +//= require_tree . diff --git a/app/views/layouts/featurer_web/application.html.erb b/app/views/layouts/featurer_web/application.html.erb index 6d52dcf..71d0a35 100644 --- a/app/views/layouts/featurer_web/application.html.erb +++ b/app/views/layouts/featurer_web/application.html.erb @@ -11,7 +11,6 @@ <%= stylesheet_link_tag "https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css", media: "all" %> <%= stylesheet_link_tag "featurer_web/application", media: "all" %> - <%= javascript_include_tag "https://code.jquery.com/jquery-3.1.1.min.js" %> <%= javascript_include_tag "featurer_web/application" %> diff --git a/featurer_web.gemspec b/featurer_web.gemspec index cdc396a..e7d4c1b 100644 --- a/featurer_web.gemspec +++ b/featurer_web.gemspec @@ -19,4 +19,5 @@ Gem::Specification.new do |s| s.add_dependency 'rails', '>= 4.2.7' s.add_dependency 'featurer', '~> 0.1.1' + s.add_dependency 'jquery-rails', '>= 1.0' end diff --git a/lib/featurer_web/engine.rb b/lib/featurer_web/engine.rb index 12b21d0..a7764e4 100644 --- a/lib/featurer_web/engine.rb +++ b/lib/featurer_web/engine.rb @@ -1,5 +1,6 @@ module FeaturerWeb class Engine < ::Rails::Engine + require 'jquery-rails' isolate_namespace FeaturerWeb end end