From b2f41f16c36f8c2a7fdb8a5c06635bfc3ac610d9 Mon Sep 17 00:00:00 2001 From: Calvin Armstrong <138508650+armstrca@users.noreply.github.com> Date: Wed, 11 Oct 2023 22:10:06 +0000 Subject: [PATCH 1/5] CRUD done maybe should do likes/follows next --- app/controllers/comments_controller.rb | 34 ++++++++++++------- app/views/comments/_comment.html.erb | 10 ++++-- app/views/comments/_form.html.erb | 4 +-- app/views/comments/create.js.erb | 9 ++++++ app/views/comments/destroy.js.erb | 3 ++ app/views/comments/edit.js.erb | 1 + app/views/comments/update.js.erb | 1 + config/importmap.rb | 2 +- domain modeling notes | 45 ++++++++++++++++++++++++++ 9 files changed, 91 insertions(+), 18 deletions(-) create mode 100644 app/views/comments/create.js.erb create mode 100644 app/views/comments/destroy.js.erb create mode 100644 app/views/comments/edit.js.erb create mode 100644 app/views/comments/update.js.erb create mode 100644 domain modeling notes diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index 5b62df9e..e2c3acaa 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 @@ -33,9 +34,11 @@ 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 } + format.js end end end @@ -46,9 +49,11 @@ 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 } + format.js end end end @@ -59,23 +64,28 @@ 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 private - # Use callbacks to share common setup or constraints between actions. - def set_comment - @comment = Comment.find(params[:id]) - end - def ensure_current_user_is_owner - if current_user != @comment.author - redirect_back fallback_location: root_url, alert: "You're not authorized for that." - end - end + # Use callbacks to share common setup or constraints between actions. + def set_comment + @comment = Comment.find(params[:id]) + end - # Only allow a list of trusted parameters through. - def comment_params - params.require(:comment).permit(:author_id, :photo_id, :body) + def ensure_current_user_is_owner + if current_user != @comment.author + redirect_back fallback_location: root_url, alert: "You're not authorized for that." end + end + + # Only allow a list of trusted parameters through. + def comment_params + params.require(:comment).permit(:author_id, :photo_id, :body) + end end diff --git a/app/views/comments/_comment.html.erb b/app/views/comments/_comment.html.erb index 7b8dc14d..9ba17c6c 100644 --- a/app/views/comments/_comment.html.erb +++ b/app/views/comments/_comment.html.erb @@ -1,4 +1,4 @@ -