From 2191543e1011a6ea71c747cab4f77ba0be06ecd2 Mon Sep 17 00:00:00 2001 From: YungKeeks Date: Mon, 23 Jan 2023 22:42:04 +0000 Subject: [PATCH] Destroy and Create --- app/controllers/comments_controller.rb | 2 ++ app/views/comments/_comment.html.erb | 4 ++-- app/views/comments/_form.html.erb | 4 ++-- app/views/comments/create.js.erb | 9 +++++++++ 4 files changed, 15 insertions(+), 4 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 5b62df9..29cc39c 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -36,6 +36,7 @@ def create else format.html { render :new, status: :unprocessable_entity } format.json { render json: @comment.errors, status: :unprocessable_entity } + format.js end end end @@ -59,6 +60,7 @@ 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 end end diff --git a/app/views/comments/_comment.html.erb b/app/views/comments/_comment.html.erb index 25bdd33..d9176f2 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 %>
    @@ -13,7 +13,7 @@ <% end %> - <%= link_to comment, 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 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..120b538 --- /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("");