使用CarrierWave设置自定义错误消息
我有一个在carrierwave上运行的图像上传应用程序,限制用户只上传所需扩展名的图像’jpg,jpeg,png’我已经在我的上传器中定义了载波波的validation定义为
def extension_white_list %w(jpg jpeg png) end
现在,任何尝试上传具有与所需扩展名不同的扩展名的图像(如上所述)都会导致validation错误
我想自定义validation错误消息
现在错误消息显示为
You are not allowed to upload "" files, allowed types: ["jpg","jpeg","png"]
谁能帮助我找到这个链接的人提到如何实现这一点
但它有些问题
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 (eg) carrierwave_processing_error key if error is for processing failure
请给我一个CarrierWave的答案,请不要让我为扩展匹配编写单独的validation集
实际上它是:
en: errors: messages: extension_white_list_error: "You are not allowed to upload %{extension} files, allowed types: %{allowed_types}"
资源:
https://github.com/jnicklas/carrierwave/blob/master/lib/carrierwave/locale/en.yml
如果有人在2016年以后到达这里,并想知道为什么在这个页面或网络上的任何其他地方找到的正确答案不起作用,可能是因为这个原因。 至少在我的情况下:
重命名
extension_white_list
~>extension_whitelist
复制解决方案时很容易错过。
https://github.com/carrierwaveuploader/carrierwave/commit/06003a5044190f93d07d958b6ca9fd6f6f8fbdb2
在en.yml中定义一个键值对
en: errors: messages: extension_white_list_error: 'My Custom Message'
如果最近版本已更改。 如果以上答案不起作用,请尝试如下:
en: errors: messages: extension_whitelist_error: 'My Custom Message'
让CarrierWave完成其余的工作
由于某种原因,我的ActiveModel在调用mount_uploader
时没有包含来自carrierwave的validation模块。 我必须在我的模型中include CarrierWave::Validations::ActiveModel
以获得完整性validation器。