问题描述
我有一个在carrierwave上运行的图像上传应用程序,该应用程序限制用户仅上传所需扩展名 jpg,jpeg,png的图像,我已经在上载器中定义了载波的验证定义为
I have an Image uploading application running on carrierwave which restrict user from uploading image only of desired extensions 'jpg,jpeg,png' I have put in place the validation for carrierwave in my Uploader define as
def extension_white_list
%w(jpg jpeg png)
end
现在任何试图上传扩展名与所需扩展名不同的图像的人(上面提到的)都会导致验证错误
Now anyone trying to upload image having extension different from the desired extension(mention above) will result in validation errors
我要自定义验证错误消息
I want to customize the Validation Error message
现在错误消息显示为
You are not allowed to upload "" files, allowed types: ["jpg","jpeg","png"]
有人可以帮忙吗?我找到了这个,其中的人提到了如何实现
Can anyone help I found this link where the guys mention how to achieve this
,但是有些问题
1. I18n support I dont require to translate the error message using I18n
2. The 'Key' to be used I not sure which key to used in YAML for
not matching extension whitelist error message (e.g) carrierwave_processing_error key if error is for processing failure
请以CarrierWave的术语提供我的答案,请不要请我为扩展名匹配编写单独的验证集
Please Provide me answer in term of CarrierWave and please dont ask me to write separate validation set for extension matching
推荐答案
将此添加到您的 .yml
翻译文件:
Add this to your .yml
translation file:
en:
errors:
messages:
extension_white_list_error: "You are not allowed to upload %{extension} files, allowed types: %{allowed_types}"
content_type_whitelist_error: "You are not allowed to upload %{content_type} files, allowed types: %{allowed_types}"
来源:
这篇关于使用CarrierWave设置自定义错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!