I have a weird issue. We have an image validator that prevents images smaller than a certain size being uploaded. If the image is too small then it directs the user to the /upload page with a flash error message.
It was working nicely until I made some major changes to the app; Major refactor, including moving code into modules, upgrading to rails 3.2.13 and moving from R Magick
to Mini Magick
. Quite a few things, I didn't change anything to do with the validator.
Now for some reason it doesn't redirect to the /upload page anymore with the flash message. Instead, in development, it shows the error message on the rails exception page titled "ActiveRecord::RecordInvalid in UploadController#create
". And in production, on Heroku, it responds with the 422 page, "The change you wanted was rejected".
Any ideas?
Create action in the controller
def create
@item = Item.create!(params[:item])
if @item.save
redirect_to crop_url(item_id: @item.id)
else
error_msg = @item.errors[:base]
redirect_to upload_url, flash: { errors: error_msg }
end
end
Validate method in the upload_process module
def validate_minimum_image_size
geometry = cover.geometry
if (! geometry.nil?)
width = geometry[0]
height = geometry[1]
end
unless (width >= 540 && height >= 540)
errors.add :base, "Oops! Your image is too small... Dimensions must be at least 540x540 pixels!"
end
end
Error message in the view (HAML)
.error
.pink
- if flash.present?
- if flash[:errors].present?
- flash[:errors].each do |err|
= err
You should have:
@item = Item.new(params[:item])
instead of:
@item = Item.create!(params[:item])
Error occurred because you were calling create!
- this method raises error if the validation fails.