From 310830066580e284157cd45e4b652d9150d14744 Mon Sep 17 00:00:00 2001 From: anhdao21 Date: Thu, 2 Jun 2022 18:17:14 +0000 Subject: [PATCH 1/2] lets gooo --- app/controllers/photos_controller.rb | 97 +++++++++++----------------- app/views/comments/_comment.html.erb | 7 +- app/views/comments/destroy.js.erb | 5 ++ 3 files changed, 47 insertions(+), 62 deletions(-) create mode 100644 app/views/comments/destroy.js.erb diff --git a/app/controllers/photos_controller.rb b/app/controllers/photos_controller.rb index 4a37f0e..f5dc6b1 100644 --- a/app/controllers/photos_controller.rb +++ b/app/controllers/photos_controller.rb @@ -1,81 +1,58 @@ class PhotosController < ApplicationController - before_action :set_photo, only: %i[ show edit update destroy ] - - before_action :ensure_current_user_is_owner, only: [:destroy, :update, :edit] - - # GET /photos or /photos.json def index - @photos = Photo.all + matching_photos = Photo.all + + @list_of_photos = matching_photos.order({ :created_at => :desc }) + + render({ :template => "photos/index.html.erb" }) end - # GET /photos/1 or /photos/1.json def show - end + the_id = params.fetch("path_id") - # GET /photos/new - def new - @photo = Photo.new - end + matching_photos = Photo.where({ :id => the_id }) + + @the_photo = matching_photos.at(0) - # GET /photos/1/edit - def edit + render({ :template => "photos/show.html.erb" }) end - # POST /photos or /photos.json def create - @photo = Photo.new(photo_params) - @photo.owner = current_user - - respond_to do |format| - if @photo.save - format.html { redirect_to @photo, notice: "Photo was successfully created." } - format.json { render :show, status: :created, location: @photo } - else - format.html { render :new, status: :unprocessable_entity } - format.json { render json: @photo.errors, status: :unprocessable_entity } - end + the_photo = Photo.new + the_photo.image = params.fetch("query_image") + the_photo.caption = params.fetch("query_caption") + the_photo.owner_id = params.fetch("query_owner_id") + + if the_photo.valid? + the_photo.save + redirect_to("/photos", { :notice => "Photo created successfully." }) + else + redirect_to("/photos", { :alert => the_photo.errors.full_messages.to_sentence }) end end - # PATCH/PUT /photos/1 or /photos/1.json def update - respond_to do |format| - if @photo.update(photo_params) - format.html { redirect_to @photo, notice: "Photo was successfully updated." } - format.json { render :show, status: :ok, location: @photo } - else - format.html { render :edit, status: :unprocessable_entity } - format.json { render json: @photo.errors, status: :unprocessable_entity } - end + the_id = params.fetch("path_id") + the_photo = Photo.where({ :id => the_id }).at(0) + + the_photo.image = params.fetch("query_image") + the_photo.caption = params.fetch("query_caption") + the_photo.owner_id = params.fetch("query_owner_id") + + if the_photo.valid? + the_photo.save + redirect_to("/photos/#{the_photo.id}", { :notice => "Photo updated successfully."} ) + else + redirect_to("/photos/#{the_photo.id}", { :alert => the_photo.errors.full_messages.to_sentence }) end end - # DELETE /photos/1 or /photos/1.json - - def destroy - @photo.destroy + the_id = params.fetch("path_id") + the_photo = Photo.where({ :id => the_id }).at(0) - respond_to do |format| - format.html { redirect_back fallback_location: root_url, notice: "Photo was successfully destroyed." } - format.json { head :no_content } - end - end - - private - # Use callbacks to share common setup or constraints between actions. - def set_photo - @photo = Photo.find(params[:id]) - end + the_photo.destroy - def ensure_current_user_is_owner - if current_user != @photo.owner - redirect_back fallback_location: root_url, alert: "You're not authorized for that." - end - end - - # Only allow a list of trusted parameters through. - def photo_params - params.require(:photo).permit(:image, :comments_count, :likes_count, :caption, :owner_id) - end + redirect_to("/photos", { :notice => "Photo deleted successfully."} ) + end end diff --git a/app/views/comments/_comment.html.erb b/app/views/comments/_comment.html.erb index 25bdd33..dc15a9d 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,10 @@ <% 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/destroy.js.erb b/app/views/comments/destroy.js.erb new file mode 100644 index 0000000..9834fb4 --- /dev/null +++ b/app/views/comments/destroy.js.erb @@ -0,0 +1,5 @@ +console.log("bye comment!") + +$("#<%= dom_id(@comment) %>").fadeOut(1000, function() { + $(this).remove(); +}); From ea6d1179bb26baec571cf0b0cac60642c86ecdc2 Mon Sep 17 00:00:00 2001 From: anhdao21 Date: Thu, 2 Jun 2022 19:29:38 +0000 Subject: [PATCH 2/2] "all done" --- app/controllers/comments_controller.rb | 9 +++++++++ app/views/comments/_comment.html.erb | 4 +++- app/views/comments/_form.html.erb | 4 ++-- 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index 5b62df9..9ba332e 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 @@ -37,6 +38,10 @@ def create format.html { render :new, status: :unprocessable_entity } format.json { render json: @comment.errors, status: :unprocessable_entity } end + + format.js do + render template: "comments/create.js.erb" + end end end @@ -46,6 +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 } @@ -59,6 +65,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.js.erb" + end end end diff --git a/app/views/comments/_comment.html.erb b/app/views/comments/_comment.html.erb index dc15a9d..a08a7b3 100644 --- a/app/views/comments/_comment.html.erb +++ b/app/views/comments/_comment.html.erb @@ -9,7 +9,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), + class: "btn btn-link btn-sm text-muted", + remote: true do %> <% end %> diff --git a/app/views/comments/_form.html.erb b/app/views/comments/_form.html.erb index f289224..b335e0b 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? %>