From 0d0880fb41e094ff6cd7a8380451f747f73061e0 Mon Sep 17 00:00:00 2001 From: jason ho <43012595+JasonHo404@users.noreply.github.com> Date: Wed, 25 Oct 2023 15:54:23 +0000 Subject: [PATCH 1/3] ajaxified delete --- app/controllers/comments_controller.rb | 3 +++ app/views/comments/_comment.html.erb | 6 +++--- app/views/comments/destroy.js.erb | 3 +++ 3 files changed, 9 insertions(+), 3 deletions(-) create mode 100644 app/views/comments/destroy.js.erb diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index 5b62df9e..e87574d5 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -59,6 +59,9 @@ 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..ae01d44e 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 %> @@ -11,11 +11,11 @@
    <% if current_user == comment.author %> - <%= link_to edit_comment_path(comment), class: "btn btn-link btn-sm text-muted" do %> + <%= link_to edit_comment_path(comment), class: "btn btn-link btn-sm text-muted", remote: true do %> <% 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/destroy.js.erb b/app/views/comments/destroy.js.erb new file mode 100644 index 00000000..1c5f5ed7 --- /dev/null +++ b/app/views/comments/destroy.js.erb @@ -0,0 +1,3 @@ +$("#<%= dom_id(@comment) %>").fadeOut(500, function() { + $(this).remove(); +}); From 847e28f240062d62ccd02a68e99588563ce6ccbe Mon Sep 17 00:00:00 2001 From: jason ho <43012595+JasonHo404@users.noreply.github.com> Date: Wed, 25 Oct 2023 20:56:56 +0000 Subject: [PATCH 2/3] ajaxified edit and update --- app/controllers/comments_controller.rb | 9 +++++++++ app/views/comments/_form.html.erb | 4 ++-- app/views/comments/create.js.erb | 7 +++++++ app/views/comments/edit.js.erb | 5 +++++ app/views/comments/update.js.erb | 2 ++ 5 files changed, 25 insertions(+), 2 deletions(-) create mode 100644 app/views/comments/create.js.erb create mode 100644 app/views/comments/edit.js.erb create mode 100644 app/views/comments/update.js.erb diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index e87574d5..fc4b6a3a 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -21,6 +21,9 @@ def new def edit respond_to do |format| format.html + format.js do + render template: "comments/edit" + end end end @@ -33,6 +36,9 @@ 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 do + render template: "comments/create" + end else format.html { render :new, status: :unprocessable_entity } format.json { render json: @comment.errors, status: :unprocessable_entity } @@ -46,6 +52,9 @@ def update if @comment.update(comment_params) format.html { redirect_to root_url, notice: "Comment was successfully updated." } format.json { render :show, status: :ok, location: @comment } + format.js do + render template: "comments/update" + end else format.html { render :edit, status: :unprocessable_entity } format.json { render json: @comment.errors, status: :unprocessable_entity } diff --git a/app/views/comments/_form.html.erb b/app/views/comments/_form.html.erb index 4917fb32..25ad8897 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? %>