From f2f0e6b517d78549aceeb92422ae85c167165b5e Mon Sep 17 00:00:00 2001 From: MosheKP2022 Date: Mon, 2 May 2022 02:42:23 +0000 Subject: [PATCH 1/2] edited delete and new comment --- app/controllers/comments_controller.rb | 8 ++++++++ app/views/comments/_comment.html.erb | 7 +++++-- app/views/comments/_form.html.erb | 4 ++-- app/views/comments/create.js.erb | 12 ++++++++++++ app/views/comments/destroy.js.erb | 5 +++++ 5 files changed, 32 insertions(+), 4 deletions(-) create mode 100644 app/views/comments/create.js.erb 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..204ab7d 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -33,6 +33,10 @@ 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 + # format.js do + # render("comments/create.js.erb") + # end else format.html { render :new, status: :unprocessable_entity } format.json { render json: @comment.errors, status: :unprocessable_entity } @@ -46,6 +50,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 } + else format.html { render :edit, status: :unprocessable_entity } format.json { render json: @comment.errors, status: :unprocessable_entity } @@ -59,6 +64,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("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..53ce1ce 100644 --- a/app/views/comments/_comment.html.erb +++ b/app/views/comments/_comment.html.erb @@ -1,4 +1,5 @@ -
  • +
  • <%= image_tag comment.author.avatar_image, class: "rounded-circle mr-2", width: 36 %>
    @@ -13,7 +14,9 @@ <% 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/_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..f45ca19 --- /dev/null +++ b/app/views/comments/create.js.erb @@ -0,0 +1,12 @@ +console.log("<%= @comment.body %>") + + +var added_comment = $("<%= escape_javascript(render 'comments/comment', comment: @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/comments/destroy.js.erb b/app/views/comments/destroy.js.erb new file mode 100644 index 0000000..ac74568 --- /dev/null +++ b/app/views/comments/destroy.js.erb @@ -0,0 +1,5 @@ +console.log("bye comment!") + +$("#<%= dom_id(@comment) %>").fadeOut(300, function() { + $(this).remove(); +}); From 354b1dae65021bc3989a1ef42a90e8709a6be209 Mon Sep 17 00:00:00 2001 From: MosheKP2022 Date: Mon, 2 May 2022 03:42:40 +0000 Subject: [PATCH 2/2] edited edit and update comment --- app/controllers/comments_controller.rb | 3 ++- app/views/comments/_comment.html.erb | 4 +++- app/views/comments/edit.js.erb | 7 +++++++ app/views/comments/update.js.erb | 11 +++++++++++ 4 files changed, 23 insertions(+), 2 deletions(-) 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 204ab7d..ed2828c 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 @@ -50,7 +51,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/_comment.html.erb b/app/views/comments/_comment.html.erb index 53ce1ce..190b323 100644 --- a/app/views/comments/_comment.html.erb +++ b/app/views/comments/_comment.html.erb @@ -10,7 +10,9 @@
      <% 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..7f8b188 --- /dev/null +++ b/app/views/comments/edit.js.erb @@ -0,0 +1,7 @@ +console.log("let's start edit the comment!") + +$("#<%= dom_id(@comment) %>").remove(); + +var edited_comment = $("<%= escape_javascript(render 'form', comment: @comment) %>"); + +$("#<%= dom_id(@comment.photo) %>_new_comment_form").before(edited_comment); diff --git a/app/views/comments/update.js.erb b/app/views/comments/update.js.erb new file mode 100644 index 0000000..fda8a0e --- /dev/null +++ b/app/views/comments/update.js.erb @@ -0,0 +1,11 @@ +console.log("let's update the comment!") + +var updated_comment = $("<%= escape_javascript(render 'comments/comment', comment: @comment) %>"); + +updated_comment.hide(); + +$("#<%= dom_id(@comment.photo) %>_new_comment_form").before(updated_comment); + +updated_comment.slideDown(); + +$("#<%= dom_id(@comment.photo) %>_new_comment_form").remove();