diff --git a/backend/app/controllers/spree/admin/resource_controller.rb b/backend/app/controllers/spree/admin/resource_controller.rb index b1699b294df..8406cf97d50 100644 --- a/backend/app/controllers/spree/admin/resource_controller.rb +++ b/backend/app/controllers/spree/admin/resource_controller.rb @@ -6,6 +6,7 @@ class Spree::Admin::ResourceController < Spree::Admin::BaseController helper_method :new_object_url, :edit_object_url, :object_url, :collection_url before_action :load_resource, except: :update_positions rescue_from ActiveRecord::RecordNotFound, with: :resource_not_found + rescue_from ActiveRecord::RecordInvalid, with: :resource_invalid respond_to :html @@ -299,4 +300,19 @@ def render_after_create_error def render_after_update_error render action: 'edit' end + + def resource_invalid(exception) + invoke_callbacks(action, :fails) + respond_with(@object) do |format| + format.html do + flash.now[:error] = exception.message + if @object.new_record? + render_after_create_error + else + render_after_update_error + end + end + format.js { render layout: false } + end + end end diff --git a/backend/spec/controllers/spree/admin/resource_controller_spec.rb b/backend/spec/controllers/spree/admin/resource_controller_spec.rb index 4d9dec300d3..7ac5565a860 100644 --- a/backend/spec/controllers/spree/admin/resource_controller_spec.rb +++ b/backend/spec/controllers/spree/admin/resource_controller_spec.rb @@ -141,6 +141,17 @@ def check_destroy_constraints expect(flash[:error]).to eq assigns(:widget).errors.full_messages.join(', ') end end + + context 'resource invalid' do + before do + allow_any_instance_of(Widget).to receive(:update).and_raise(ActiveRecord::RecordInvalid) + end + + it 'returns to edit page with error' do + put :update, params: params + expect(flash[:error]).to eq('Record invalid') + end + end end describe '#destroy' do