From 5f603e27059d7c8b7b6a8946e1e61efeab3c8ea3 Mon Sep 17 00:00:00 2001 From: Samuel Lubliner <74507133+Samuel-Lubliner@users.noreply.github.com> Date: Tue, 31 Oct 2023 21:02:47 +0000 Subject: [PATCH 1/2] ajax create and delete comment --- .rake_tasks~ | 74 ++++++++++++++++++++++++++ app/controllers/comments_controller.rb | 5 ++ app/views/comments/_comment.html.erb | 7 ++- app/views/comments/_form.html.erb | 4 +- app/views/comments/create.js.erb | 9 ++++ app/views/comments/destroy.js.erb | 5 ++ 6 files changed, 100 insertions(+), 4 deletions(-) create mode 100644 .rake_tasks~ create mode 100644 app/views/comments/create.js.erb create mode 100644 app/views/comments/destroy.js.erb diff --git a/.rake_tasks~ b/.rake_tasks~ new file mode 100644 index 00000000..4ee8ecb1 --- /dev/null +++ b/.rake_tasks~ @@ -0,0 +1,74 @@ +about +action_mailbox:ingress:exim +action_mailbox:ingress:postfix +action_mailbox:ingress:qmail +action_mailbox:install +action_mailbox:install:migrations +action_text:install +action_text:install:migrations +active_storage:install +annotate_models +annotate_routes +app:template +app:update +assets:clean[keep] +assets:clobber +assets:environment +assets:precompile +cache_digests:dependencies +cache_digests:nested_dependencies +db:create +db:drop +db:encryption:init +db:environment:set +db:fixtures:load +db:migrate +db:migrate:down +db:migrate:redo +db:migrate:status +db:migrate:up +db:prepare +db:reset +db:rollback +db:schema:cache:clear +db:schema:cache:dump +db:schema:dump +db:schema:load +db:seed +db:seed:replant +db:setup +db:version +erd +grade +grade:all +grade:next +grade:reset_token +grade_runner:runner +importmap:install +log:clear +middleware +remove_annotation +remove_routes +restart +sample_data +secret +spec +spec:features +specs:readme +stats +stimulus:install +stimulus:install:importmap +stimulus:install:node +test +test:all +test:db +test:system +time:zones[country_or_offset] +tmp:clear +tmp:create +turbo:install +turbo:install:importmap +turbo:install:node +turbo:install:redis +yarn:install +zeitwerk:check diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index 5b62df9e..d7cd7928 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -33,6 +33,7 @@ def create if @comment.save format.html { redirect_back fallback_location: root_path, notice: "Comment was successfully created." } format.json { render :show, status: :created, location: @comment } + format.js else format.html { render :new, status: :unprocessable_entity } format.json { render json: @comment.errors, status: :unprocessable_entity } @@ -59,6 +60,10 @@ def destroy respond_to do |format| format.html { redirect_back fallback_location: root_url, notice: "Comment was successfully destroyed." } format.json { head :no_content } + + format.js do + render template: "comments/destroy" + end end end diff --git a/app/views/comments/_comment.html.erb b/app/views/comments/_comment.html.erb index 7b8dc14d..9c2f2a77 100644 --- a/app/views/comments/_comment.html.erb +++ b/app/views/comments/_comment.html.erb @@ -1,4 +1,4 @@ -
  • +
  • <%= image_tag comment.author.avatar_image, class: "rounded-circle mr-2", width: 36 %> @@ -15,7 +15,10 @@ <% end %> - <%= link_to comment, data: { turbo_method: :delete }, class: "btn btn-link btn-sm text-muted" do %> + <%= link_to comment, + method: :delete, + class: "btn btn-link btn-sm text-muted", + remote: true do %> <% end %> <% end %> diff --git a/app/views/comments/_form.html.erb b/app/views/comments/_form.html.erb index 4917fb32..7ce9cb6b 100644 --- a/app/views/comments/_form.html.erb +++ b/app/views/comments/_form.html.erb @@ -1,5 +1,5 @@ -
  • - <%= form_with(model: comment) do |form| %> +
  • + <%= form_with(model: comment, local: false) do |form| %> <% if comment.errors.any? %>