更新时间:2022-12-07 09:52:32
看来,验证是提高仅仅是添加错误信息到错误堆栈的错误信息。我建议你包裹 update_attributes的块在
开始/救援/ end`块。
It appears that the validator is raising an error instead of simply adding error messages to the errors stack. I'd recommend wrapping the update_attributes block in a
begin/rescue/end` block.
有关更改邮件,你可以采取两种途径:
For changing the message, you can take two routes:
您可以自定义 EnumValidator
使用键从区域文件,使每个条目可能会说的元素%{数}不在列表。您应该能够自定义 activerecord.errors.messages.inclusion
该消息的不在名单部分。对于元素0(导轨)部分中,您既可以硬$ C C语$,作为目前执行(不推荐)或使用的语言环境的文件再次通过添加键说, enum_element
,然后在您的EnumValidator,电话 I18n.translate(:'activerecord.errors.messages.enum_element,数:@count,值:值)
。
You could customize the EnumValidator
to use keys from your locale file so that each entry might say something like the "element %{count} is not in the list". You should be able to customize activerecord.errors.messages.inclusion
for the "is not in the list" portion of the message. For the "element 0 (rails)" portion, you could either hard-code the phrase, as is currently implemented (not recommended) or make use of the locale file again by adding a key for say, enum_element
, then in your EnumValidator, call I18n.translate(:'activerecord.errors.messages.enum_element', count: @count, value: value)
.
验证后,检查 errors.keys
为您的枚举列问题。如果您发现错误,删除那些键,并添加自己的消息对于这把钥匙。
After validation, check errors.keys
for for issues on your enumerable column. If you find errors, remove those keys and add your own message for that key.
至于你的形式,这听起来像您使用的是动态表单生成器。如果是SimpleForm,你可以尝试指定的确切类型的输入应与为::选择
选项
As for your form, it sounds like you're using a dynamic form builder. If it's SimpleForm, you could try specifying the exact type the input should be with the as: :select
option.