comment creation

This commit is contained in:
Mark Moser 2017-02-13 15:54:57 -06:00
parent 567b4a409d
commit da5dc4bd94
5 changed files with 50 additions and 3 deletions

View File

@ -0,0 +1,26 @@
# frozen_string_literal: true
module Admin
class CommentController < AdminController
before_action { authorize QuizComment }
def create
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
end
private
def comment_params
params.require(:quiz_comment).permit(:message, :id)
end
end
end

View File

@ -1,6 +1,6 @@
<%= render partial: 'shared/form_model_errors', locals: { obj: @comment } %> <%= render partial: 'shared/form_model_errors', locals: { obj: @comment } %>
<%= form_for @comment, url: "#", method: :post do |form| %> <%= form_for @comment, url: admin_create_comment_path(test_hash), method: :post do |form| %>
<div class="form-group"> <div class="form-group">
<%= form.label :message, "Comment" %> <%= form.label :message, "Comment" %>
<%= form.text_area :message %> <%= form.text_area :message %>

View File

@ -45,7 +45,7 @@
<div class="review-comments"> <div class="review-comments">
<h2 class="prft-heading">Comments</h2> <h2 class="prft-heading">Comments</h2>
<%= render partial: 'comment', collection: @comments %> <%= render partial: 'comment', collection: @comments, locals: { test_hash: @candidate.test_hash } %>
<div><%= render partial: 'comment_form' %></div> <div><%= render partial: 'comment_form', locals: { test_hash: @candidate.test_hash } %></div>
</div> </div>
</div> </div>

View File

@ -52,6 +52,8 @@ Rails.application.routes.draw do
get "/admin/results", to: "admin/result#index", as: :admin_results get "/admin/results", to: "admin/result#index", as: :admin_results
get "/admin/result/:test_hash", to: "admin/result#view", as: :admin_result get "/admin/result/:test_hash", to: "admin/result#view", as: :admin_result
post "/admin/comment/:test_hash", to: "admin/comment#create", as: :admin_create_comment
get "admin/vote/:test_hash/up", to: "admin/vote#up", as: :admin_up_vote, defaults: { format: 'json' } get "admin/vote/:test_hash/up", to: "admin/vote#up", as: :admin_up_vote, defaults: { format: 'json' }
get "admin/vote/:test_hash/down", to: "admin/vote#down", as: :admin_down_vote, defaults: { format: 'json' } get "admin/vote/:test_hash/down", to: "admin/vote#down", as: :admin_down_vote, defaults: { format: 'json' }
get "admin/vote/:test_hash/approve", to: "admin/vote#approve", as: :admin_approve_vote, defaults: { format: 'json' } get "admin/vote/:test_hash/approve", to: "admin/vote#approve", as: :admin_approve_vote, defaults: { format: 'json' }

View File

@ -0,0 +1,19 @@
# frozen_string_literal: true
require 'test_helper'
module Admin
class CommentControllerTest < ActionDispatch::IntegrationTest
test "should post create" do
auth_reviewer
candidate = candidates(:stacy)
assert_difference("QuizComment.count") do
post admin_create_comment_url(test_hash: candidate.test_hash), params: { quiz_comment: {
message: 'this is a test comment'
} }
end
assert_redirected_to admin_result_url(test_hash: candidate.test_hash)
assert flash[:success]
end
end
end