问题
here is a similar case but no solution CarrierWave extension_white_list doesn't seem to work
Carrierwave provides a function extension_white_list
which is supposed to prevent a user from uploading an invalid file. I have uncommented this function but how do I handle this case? I want to catch this error and inform the user that he should change the file.
this wiki entry seems relevant but I'm not sure how to proceed https://github.com/carrierwaveuploader/carrierwave/wiki/How-to%3A-Validate-uploads-with-Active-Record
回答1:
According to this issue https://github.com/carrierwaveuploader/carrierwave/issues/1896
The solution is this and it worked for me;
change:
def extension_white_list
end
to:
def extension_whitelist
end
so they have removed the underscore from whitelist word.
回答2:
Inside your model, validates_integrity_of :avatar
will cause whitelist violations to act much like validates_presence_of :another_field
would with a blank field when saving, i.e. save
will return false, after which valid?
will be false and there will be a pertinent error message available in user.errors[:avatar]
.
来源:https://stackoverflow.com/questions/19043926/carrierwave-extension-white-list-not-working