Move action to create answers to AnswersController

It was confusing to have the action to create an answer in
`QuestionsController#answer` while the action to destroy it was
`AnswersController#destroy`.
This commit is contained in:
Javi Martín
2024-05-14 01:49:56 +02:00
parent 9a840bb8d1
commit 03f89c9ca2
5 changed files with 21 additions and 22 deletions

View File

@@ -11,7 +11,7 @@
"aria-pressed": true %>
<% else %>
<%= button_to question_option.title,
answer_question_path(question, answer: question_option.title),
question_answers_path(question, answer: question_option.title),
remote: true,
title: t("poll_questions.show.vote_answer", answer: question_option.title),
class: "button secondary hollow",

View File

@@ -2,7 +2,24 @@ class Polls::AnswersController < ApplicationController
load_and_authorize_resource :question, class: "::Poll::Question"
load_and_authorize_resource :answer, class: "::Poll::Answer",
through: :question,
through_association: :answers
through_association: :answers,
only: :destroy
def create
authorize! :answer, @question
answer = @question.find_or_initialize_user_answer(current_user, params[:answer])
answer.save_and_record_voter_participation
respond_to do |format|
format.html do
redirect_to request.referer
end
format.js do
render :show
end
end
end
def destroy
@answer.destroy_and_remove_voter_participation
@@ -12,7 +29,7 @@ class Polls::AnswersController < ApplicationController
redirect_to request.referer
end
format.js do
render "polls/questions/options"
render :show
end
end
end

View File

@@ -1,17 +0,0 @@
class Polls::QuestionsController < ApplicationController
load_and_authorize_resource :question, class: "Poll::Question"
def answer
answer = @question.find_or_initialize_user_answer(current_user, params[:answer])
answer.save_and_record_voter_participation
respond_to do |format|
format.html do
redirect_to request.referer
end
format.js do
render :options
end
end
end
end

View File

@@ -5,8 +5,7 @@ resources :polls, only: [:show, :index] do
end
resources :questions, controller: "polls/questions", shallow: true, only: [] do
post :answer, on: :member
resources :answers, controller: "polls/answers", only: :destroy, shallow: false
resources :answers, controller: "polls/answers", only: [:create, :destroy], shallow: false
end
end