File uploading with Rails is a topic that’s really hot for my readers. Mike asked the other day, how he could do validations for uploaded files.
There are certainly many ways and it really depends on your specific setup and what you would like to validate. So this can only be a small hint in a right direction:
Modify the controller’s response
When we try to create the file, the controller renders
create.js.erb. The file could look like this before we validate.
1 2 3 4 5 6 7
Upload is invalid, the model will have error messages set. You can retrieve them by calling
@upload.errors.full_messages. We just have to write them to the screen.
1 2 3 4 5 6 7 8 9
Perhaps we need to do this more often than for this one form. That’s why I use a shared partial
_error_messages.js.erb. It looks like this.
1 2 3 4 5 6 7 8 9 10
So now, when you try to upload an invalid file, you will get your errors printed to the screen.
Thank you for reading.