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:
@@ -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
|
||||
|
||||
@@ -7,8 +7,6 @@
|
||||
<%= f.hidden_field :given_order,
|
||||
value: @answer.persisted? ? @answer.given_order : @answer.class.last_position(@answer.question_id || @question.id) + 1 %>
|
||||
|
||||
<%= f.hidden_field :question_id, value: @answer.question_id || @question.id %>
|
||||
|
||||
<div class="row">
|
||||
<%= f.translatable_fields do |translations_form| %>
|
||||
<div class="column end">
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
|
||||
<div class="poll-question-form">
|
||||
<%= form_for(Poll::Question::Answer.new,
|
||||
url: admin_answer_path(@answer),
|
||||
url: admin_question_answer_path(@answer.question, @answer),
|
||||
method: :put) do |f| %>
|
||||
|
||||
<%= render "shared/errors", resource: @answer %>
|
||||
|
||||
@@ -10,5 +10,5 @@
|
||||
</h2>
|
||||
|
||||
<div class="poll-question-answer-form">
|
||||
<%= render "form", form_url: admin_answer_path(@answer) %>
|
||||
<%= render "form", form_url: admin_question_answer_path(@question, @answer) %>
|
||||
</div>
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
<%= back_link_to %>
|
||||
|
||||
<%= link_to t("admin.answers.show.edit"), edit_admin_answer_path(@answer),
|
||||
<%= link_to t("admin.answers.show.edit"), edit_admin_question_answer_path(@answer.question, @answer),
|
||||
class: "button hollow float-right" %>
|
||||
|
||||
<ul class="breadcrumbs margin-top">
|
||||
|
||||
@@ -52,7 +52,7 @@
|
||||
<tbody class="sortable" data-js-url="<%= admin_question_answers_order_answers_path(@question.id) %>">
|
||||
<% @question.question_answers.each do |answer| %>
|
||||
<tr id="<%= dom_id(answer) %>" class="poll_question_answer" data-answer-id="<%= answer.id %>">
|
||||
<td class="align-top"><%= link_to answer.title, admin_answer_path(answer) %></td>
|
||||
<td class="align-top"><%= link_to answer.title, admin_question_answer_path(@question, answer) %></td>
|
||||
<td class="align-top break"><%= wysiwyg(answer.description) %></td>
|
||||
<td class="align-top text-center">
|
||||
(<%= answer.images.count %>)
|
||||
|
||||
@@ -169,7 +169,8 @@ namespace :admin do
|
||||
end
|
||||
|
||||
resources :questions, shallow: true do
|
||||
resources :answers, except: [:index, :destroy], controller: "questions/answers" do
|
||||
resources :answers, except: [:index, :destroy], controller: "questions/answers", shallow: false
|
||||
resources :answers, only: [], controller: "questions/answers" do
|
||||
resources :images, controller: "questions/answers/images"
|
||||
resources :videos, controller: "questions/answers/videos"
|
||||
get :documents, to: "questions/answers#documents"
|
||||
|
||||
@@ -36,7 +36,7 @@ describe "Answers", :admin do
|
||||
answer = create(:poll_question_answer, question: question, title: "Answer title", given_order: 2)
|
||||
create(:poll_question_answer, question: question, title: "Another title", given_order: 1)
|
||||
|
||||
visit admin_answer_path(answer)
|
||||
visit admin_question_answer_path(question, answer)
|
||||
|
||||
click_link "Edit answer"
|
||||
|
||||
|
||||
@@ -216,7 +216,7 @@ describe "Admin edit translatable records", :admin do
|
||||
|
||||
context "CKEditor fields" do
|
||||
let(:translatable) { create(:poll_question_answer) }
|
||||
let(:path) { edit_admin_answer_path(translatable) }
|
||||
let(:path) { edit_admin_question_answer_path(translatable.question, translatable) }
|
||||
|
||||
scenario "Changes the existing translation" do
|
||||
visit path
|
||||
|
||||
Reference in New Issue
Block a user