Question

When the image gets saved, the height and width of the image get saved in the model correctly. I have a requirement where I need to validate the dimensions of the image before save. It has to be of precisely the same dimensions. But when I try to access height or width before save, it gives me nil.

Is there any way where I can achieve this?

For now, I give a warning after saving, that the image is not of the desired dimensions. But thats not neat enough.

Was it helpful?

Solution 2

I used after_validation. before_validation did not work for me. So what I essentially did was I checked the dimensions in an after_validation hook and had it put errors using @image.errors.add_to_base("The image needs to be of precisely 137x52 pixels."). And I saved the image in the following manner.

@image.valid? # Run the validations, which calls my hook method after_validation
@image.save if @image.errors.empty?

OTHER TIPS

I'm not sure about attachment_fu, but the real image probably is save in a before_save or after_save. Your validation run before the images is saved.

Use a before_validation callback to force processing of the image.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top