diff --git a/app/controllers/admin/comment_controller.rb b/app/controllers/admin/comment_controller.rb index d02ee24..cef5cff 100644 --- a/app/controllers/admin/comment_controller.rb +++ b/app/controllers/admin/comment_controller.rb @@ -1,26 +1,37 @@ # frozen_string_literal: true module Admin class CommentController < AdminController - before_action { authorize QuizComment } + def update + comment = QuizComment.find_by(id: params[:id], test_hash: params[:test_hash]) + authorize comment + + comment.update(comment_params) + flash_message = if comment.save + { success: "Sucessfully updated comment" } + else + { error: "Failed to update comment" } + end + redirect_to admin_result_path(params[:test_hash]), flash: flash_message + end def create + authorize QuizComment comment = QuizComment.create( comment_params.merge(user_id: current_user.id, test_hash: params[:test_hash]) ) - if comment.persisted? - redirect_to admin_result_path(params[:test_hash]), - flash: { success: "Sucessfully created comment" } - else - redirect_to admin_result_path(params[:test_hash]), - flash: { error: "Failed to save comment" } - end + flash_message = if comment.persisted? + { success: "Sucessfully created comment" } + else + { error: "Failed to save comment" } + end + redirect_to admin_result_path(params[:test_hash]), flash: flash_message end private def comment_params - params.require(:quiz_comment).permit(:message, :id) + params.require(:quiz_comment).permit(:message) end end end diff --git a/app/views/admin/result/_comment_form.html.erb b/app/views/admin/result/_comment_form.html.erb index d4c1b55..c2ab89c 100644 --- a/app/views/admin/result/_comment_form.html.erb +++ b/app/views/admin/result/_comment_form.html.erb @@ -1,5 +1,6 @@ <%= render partial: 'shared/form_model_errors', locals: { obj: @comment } %> +<% # admin_update_comment_path # admin_create_comment_path %> <%= form_for @comment, url: admin_create_comment_path(test_hash), method: :post do |form| %>