Skip to content

Commit

Permalink
Set flash error when unsuccessfully destroying resource
Browse files Browse the repository at this point in the history
Prior to this commit, the user would not get any error message when
trying to destroy a resource using HTML instead of JS. This fixes that.

It also removes a reference to the shared `destroy.js.erb` partial by
only populating the flash hash in the HTML case, and just sending a
plain response in the JS one.
  • Loading branch information
mamhoff committed Nov 15, 2019
1 parent b16eb0a commit 38f17e5
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
21 changes: 16 additions & 5 deletions backend/app/controllers/spree/admin/resource_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,16 +99,27 @@ def destroy

if destroy_result
invoke_callbacks(:destroy, :after)
flash[:success] = flash_message_for(@object, :successfully_removed)
respond_with(@object) do |format|
format.html { redirect_to location_after_destroy }
format.js { render partial: "spree/admin/shared/destroy" }
message = flash_message_for(@object, :successfully_removed)
format.html do
flash[:success] = message
redirect_to location_after_destroy
end
format.js do
render plain: message
end
end
else
invoke_callbacks(:destroy, :fails)
respond_with(@object) do |format|
format.html { redirect_to location_after_destroy }
format.js { render status: :unprocessable_entity, plain: @object.errors.full_messages.to_sentence }
message = @object.errors.full_messages.to_sentence
format.html do
flash[:error] = message
redirect_to location_after_destroy
end
format.js do
render status: :unprocessable_entity, plain: message
end
end
end
end
Expand Down
10 changes: 10 additions & 0 deletions backend/spec/controllers/spree/admin/resource_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,16 @@ def check_destroy_constraints
expect(response.body).to eq assigns(:widget).errors.full_messages.to_sentence
end
end

context 'html format' do
subject { delete :destroy, params: params }

it 'responds with error message' do
subject
expect(response).to be_redirect
expect(flash[:error]).to eq assigns(:widget).errors.full_messages.to_sentence
end
end
end
end

Expand Down

0 comments on commit 38f17e5

Please sign in to comment.