From 3836096fd68039b08aad5ed479df9d802d5844af Mon Sep 17 00:00:00 2001 From: lepst0928 Date: Tue, 26 Apr 2022 20:47:56 +0000 Subject: [PATCH 1/5] ajax bye comment --- app/controllers/comments_controller.rb | 4 ++++ app/views/comments/_comment.html.erb | 2 +- app/views/comments/destroy.js.erb | 1 + app/views/shared/_cdn_assets.html.erb | 2 ++ 4 files changed, 8 insertions(+), 1 deletion(-) create mode 100644 app/views/comments/destroy.js.erb diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index 5b62df9..2bee140 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -59,6 +59,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.js.erb" + end end end diff --git a/app/views/comments/_comment.html.erb b/app/views/comments/_comment.html.erb index 25bdd33..815743a 100644 --- a/app/views/comments/_comment.html.erb +++ b/app/views/comments/_comment.html.erb @@ -13,7 +13,7 @@ <% end %> - <%= link_to comment, method: :delete, class: "btn btn-link btn-sm text-muted" do %> + <%= link_to comment, method: :delete, remote: true, class: "btn btn-link btn-sm text-muted" do %> <% end %> <% end %> diff --git a/app/views/comments/destroy.js.erb b/app/views/comments/destroy.js.erb new file mode 100644 index 0000000..b8cbfe5 --- /dev/null +++ b/app/views/comments/destroy.js.erb @@ -0,0 +1 @@ +console.log("bye comment!") diff --git a/app/views/shared/_cdn_assets.html.erb b/app/views/shared/_cdn_assets.html.erb index c1dc237..9375f70 100644 --- a/app/views/shared/_cdn_assets.html.erb +++ b/app/views/shared/_cdn_assets.html.erb @@ -6,3 +6,5 @@ + + From 2167894d11c13a5f63d56ed8eae7937857a655fc Mon Sep 17 00:00:00 2001 From: lepst0928 Date: Tue, 26 Apr 2022 20:57:22 +0000 Subject: [PATCH 2/5] comment delete with fade --- app/views/comments/_comment.html.erb | 2 +- app/views/comments/destroy.js.erb | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/app/views/comments/_comment.html.erb b/app/views/comments/_comment.html.erb index 815743a..001c292 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 %>
    diff --git a/app/views/comments/destroy.js.erb b/app/views/comments/destroy.js.erb index b8cbfe5..fa3496d 100644 --- a/app/views/comments/destroy.js.erb +++ b/app/views/comments/destroy.js.erb @@ -1 +1,5 @@ console.log("bye comment!") + +$("#<%= dom_id(@comment) %>").fadeOut(100, function() { + $(this).remove(); +}); From f48dad137def70622ab2b05bd24c4e952d1591dc Mon Sep 17 00:00:00 2001 From: lepst0928 Date: Tue, 26 Apr 2022 21:28:00 +0000 Subject: [PATCH 3/5] something messed up --- app/controllers/comments_controller.rb | 2 ++ app/views/comments/_form.html.erb | 4 ++-- app/views/comments/create.js.erb | 9 +++++++++ app/views/photos/_form.html.erb | 2 +- 4 files changed, 14 insertions(+), 3 deletions(-) create mode 100644 app/views/comments/create.js.erb diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index 2bee140..007f773 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -33,6 +33,8 @@ 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 } diff --git a/app/views/comments/_form.html.erb b/app/views/comments/_form.html.erb index f289224..2a10c2e 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? %>
      diff --git a/app/views/comments/create.js.erb b/app/views/comments/create.js.erb new file mode 100644 index 0000000..2379922 --- /dev/null +++ b/app/views/comments/create.js.erb @@ -0,0 +1,9 @@ +var added_comment = $("<%= j(render @comment) %>"); + +added_comment.hide(); + +$("#<%= dom_id(@comment.photo) %>_new_comment_form").before(added_comment); + +added_comment.slideDown(); + +$("#<%= dom_id(@comment.photo) %>_new_comment_form #comment_body").val(""); diff --git a/app/views/photos/_form.html.erb b/app/views/photos/_form.html.erb index d7a268c..2552e81 100644 --- a/app/views/photos/_form.html.erb +++ b/app/views/photos/_form.html.erb @@ -1,4 +1,4 @@ -<%= form_with(model: photo) do |form| %> + <%= form_with(model: photo) do |form| %> <% if photo.errors.any? %>
        From 9b448484efbc8cad1522cc017e329f2cdbad7a63 Mon Sep 17 00:00:00 2001 From: lepst0928 Date: Wed, 27 Apr 2022 18:52:06 +0000 Subject: [PATCH 4/5] edit --- app/controllers/comments_controller.rb | 1 + app/views/comments/_comment.html.erb | 2 +- app/views/comments/edit.js.erb | 2 ++ 3 files changed, 4 insertions(+), 1 deletion(-) create mode 100644 app/views/comments/edit.js.erb diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index 007f773..ae571ff 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -21,6 +21,7 @@ def new def edit respond_to do |format| format.html + format.js end end diff --git a/app/views/comments/_comment.html.erb b/app/views/comments/_comment.html.erb index 001c292..e6d51c0 100644 --- a/app/views/comments/_comment.html.erb +++ b/app/views/comments/_comment.html.erb @@ -9,7 +9,7 @@
        <% 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), remote: true, class: "btn btn-link btn-sm text-muted" do %> <% end %> diff --git a/app/views/comments/edit.js.erb b/app/views/comments/edit.js.erb new file mode 100644 index 0000000..2780594 --- /dev/null +++ b/app/views/comments/edit.js.erb @@ -0,0 +1,2 @@ +$("#<%= dom_id(@comment) %>").replaceWith("<%= j(render "comments/form", comment: @comment) %>"); + From d8a91e9de5aded2a98e809a643a2f70a1c9593d3 Mon Sep 17 00:00:00 2001 From: lepst0928 Date: Wed, 27 Apr 2022 19:10:39 +0000 Subject: [PATCH 5/5] done --- app/controllers/comments_controller.rb | 1 + app/views/comments/_form.html.erb | 2 +- app/views/comments/update.js.erb | 1 + 3 files changed, 3 insertions(+), 1 deletion(-) create mode 100644 app/views/comments/update.js.erb diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index ae571ff..d06ac29 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -49,6 +49,7 @@ 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 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 2a10c2e..5ae0624 100644 --- a/app/views/comments/_form.html.erb +++ b/app/views/comments/_form.html.erb @@ -1,4 +1,4 @@ -
      • +
      • <%= form_with(model: comment, local: false) do |form| %> <% if comment.errors.any? %>
        diff --git a/app/views/comments/update.js.erb b/app/views/comments/update.js.erb new file mode 100644 index 0000000..650199e --- /dev/null +++ b/app/views/comments/update.js.erb @@ -0,0 +1 @@ +$("#<%= dom_id(@comment.photo) %>_<%= dom_id(@comment) %>_form").replaceWith("<%= j(render @comment) %>");