Fix crash when adding invalid documents to answers
We were rendering the `new` action, but that action doesn't exist.
Before commit ec861ca8e, we were rendering the `edit` action of an
answer, which was confusing as well.
Note that, when adding an invalid document, `@answer.documents` contains
that invalid document (which is not present in the database). Since
we're rendering the index, this new document would appear in the list of
the documents that can be deleted; to avoid that, we're kind of
"reloading" the answer object in the component by finding the record in
the database. We aren't using `@answer.reload` because doing so would
remove the validation errors.
This commit is contained in:
@@ -26,14 +26,14 @@
|
|||||||
</div>
|
</div>
|
||||||
<% end %>
|
<% end %>
|
||||||
|
|
||||||
<% if answer.documents.present? %>
|
<% if documents.present? %>
|
||||||
<table>
|
<table>
|
||||||
<tr>
|
<tr>
|
||||||
<th scope="col"><%= t("admin.questions.show.answers.document_title") %></th>
|
<th scope="col"><%= t("admin.questions.show.answers.document_title") %></th>
|
||||||
<th scope="col"><%= t("admin.questions.show.answers.document_actions") %></th>
|
<th scope="col"><%= t("admin.questions.show.answers.document_actions") %></th>
|
||||||
</tr>
|
</tr>
|
||||||
|
|
||||||
<% answer.documents.each do |document| %>
|
<% documents.each do |document| %>
|
||||||
<tr>
|
<tr>
|
||||||
<td>
|
<td>
|
||||||
<%= link_to document.title, document.attachment %>
|
<%= link_to document.title, document.attachment %>
|
||||||
|
|||||||
@@ -5,4 +5,10 @@ class Admin::Poll::Questions::Answers::Documents::IndexComponent < ApplicationCo
|
|||||||
def initialize(answer)
|
def initialize(answer)
|
||||||
@answer = answer
|
@answer = answer
|
||||||
end
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def documents
|
||||||
|
@documents ||= @answer.class.find(@answer.id).documents
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ class Admin::Poll::Questions::Answers::DocumentsController < Admin::Poll::BaseCo
|
|||||||
redirect_to admin_answer_documents_path(@answer),
|
redirect_to admin_answer_documents_path(@answer),
|
||||||
notice: t("admin.documents.create.success_notice")
|
notice: t("admin.documents.create.success_notice")
|
||||||
else
|
else
|
||||||
render :new
|
render :index
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -24,7 +24,8 @@ describe "Documents", :admin do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
scenario "Create document for answer" do
|
describe "Create document for answer" do
|
||||||
|
scenario "with valid data" do
|
||||||
answer = create(:poll_question_answer, poll: future_poll)
|
answer = create(:poll_question_answer, poll: future_poll)
|
||||||
|
|
||||||
visit admin_answer_documents_path(answer)
|
visit admin_answer_documents_path(answer)
|
||||||
@@ -36,6 +37,20 @@ describe "Documents", :admin do
|
|||||||
expect(page).to have_link "clippy.pdf"
|
expect(page).to have_link "clippy.pdf"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
scenario "with invalid data" do
|
||||||
|
answer = create(:poll_question_answer, poll: future_poll)
|
||||||
|
|
||||||
|
visit admin_answer_documents_path(answer)
|
||||||
|
|
||||||
|
documentable_attach_new_file(Rails.root.join("spec/fixtures/files/clippy.pdf"))
|
||||||
|
fill_in "Title", with: ""
|
||||||
|
click_button "Save"
|
||||||
|
|
||||||
|
expect(page).to have_content "1 error prevented this Answer from being saved"
|
||||||
|
expect(page).to have_content "Documents list"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
scenario "Remove document from answer" do
|
scenario "Remove document from answer" do
|
||||||
answer = create(:poll_question_answer, poll: future_poll)
|
answer = create(:poll_question_answer, poll: future_poll)
|
||||||
document = create(:document, documentable: answer)
|
document = create(:document, documentable: answer)
|
||||||
|
|||||||
Reference in New Issue
Block a user