diff --git a/app/controllers/spree/admin/images_controller.rb b/app/controllers/spree/admin/images_controller.rb index 6084972102..39b51149ba 100644 --- a/app/controllers/spree/admin/images_controller.rb +++ b/app/controllers/spree/admin/images_controller.rb @@ -50,19 +50,20 @@ module Spree @url_filters = ::ProductFilters.new.extract(request.query_parameters) set_viewable - if @object.update(permitted_resource_params) + if @object.update!(permitted_resource_params) flash[:success] = flash_message_for(@object, :successfully_updated) respond_with do |format| format.html { redirect_to location_after_save } format.turbo_stream end - else - respond_with(@object) end - rescue ActiveStorage::IntegrityError - @object.errors.add :attachment, :integrity_error - respond_with(@object) + rescue ActiveRecord::RecordInvalid => e + @errors = e.record.errors.map(&:full_message) + respond_with do |format| + format.html { respond_with(@object) } + format.turbo_stream { render :edit } + end end def destroy diff --git a/app/views/spree/admin/images/edit.turbo_stream.haml b/app/views/spree/admin/images/edit.turbo_stream.haml index d2ffe7dd53..b3ca87329b 100644 --- a/app/views/spree/admin/images/edit.turbo_stream.haml +++ b/app/views/spree/admin/images/edit.turbo_stream.haml @@ -3,7 +3,13 @@ %h2= t(".title") -# Display image in the same way it appears in the shopfront popup - %p= image_tag @image.persisted? ? @image.url(:large) : Spree::Image.default_image_url(:large), width: 433, height: 433 + - if defined?(@errors) && !@errors.empty? + - @errors.each do |error| + %p + = error + - else + %p= image_tag @image.persisted? ? @image.url(:large) : Spree::Image.default_image_url(:large), width: 433, height: 433 + -# Submit as turbo stream to avoid full page reload. -# TODO: show loading indicator. diff --git a/spec/requests/admin/images_spec.rb b/spec/requests/admin/images_spec.rb index e7c9584037..e77899cda7 100644 --- a/spec/requests/admin/images_spec.rb +++ b/spec/requests/admin/images_spec.rb @@ -49,8 +49,6 @@ RSpec.describe "/admin/products/:product_id/images", type: :request do product.reload }.not_to change{ product.image&.attachment&.filename.to_s } - pending "error status code" - expect(response).to be_unprocessable expect(response.body).to include "Attachment has an invalid content type" end end