Load answer through question in answers controller

We are simplifying the load answer and we can remove the ambiguous
hidden field from answer form.
This commit is contained in:
taitus
2022-09-09 15:56:36 +02:00
parent 01005b50cb
commit 405b37f605
9 changed files with 14 additions and 20 deletions

View File

@@ -2,17 +2,16 @@ class Admin::Poll::Questions::AnswersController < Admin::Poll::BaseController
include Translatable
include DocumentAttributes
before_action :load_answer, only: [:show, :edit, :update, :documents]
load_and_authorize_resource :question, class: "::Poll::Question"
load_resource class: "::Poll::Question::Answer",
through: :question,
through_association: :question_answers,
except: :documents
def new
@answer = @question.answers.new
end
def create
@answer = @question.answers.new(answer_params)
if @answer.save
redirect_to admin_question_path(@question),
notice: t("flash.actions.create.poll_question_answer")
@@ -37,6 +36,7 @@ class Admin::Poll::Questions::AnswersController < Admin::Poll::BaseController
end
def documents
@answer = ::Poll::Question::Answer.find(params[:answer_id])
@documents = @answer.documents
render "admin/poll/questions/answers/documents"
@@ -54,16 +54,11 @@ class Admin::Poll::Questions::AnswersController < Admin::Poll::BaseController
end
def allowed_params
attributes = [:title, :description, :given_order, :question_id,
documents_attributes: document_attributes]
attributes = [:title, :description, :given_order, documents_attributes: document_attributes]
[*attributes, translation_params(Poll::Question::Answer)]
end
def load_answer
@answer = ::Poll::Question::Answer.find(params[:id] || params[:answer_id])
end
def resource
load_answer unless @answer
@answer