ActionView::Base.field_error_proc = Proc.new do |html_tag, instance|
error_class = "fieldWithErrors"
if html_tag =~ /<(label)/
%Q{
<div class="#{error_class}">
#{html_tag}
<span class="error">#{instance.error_message.respond_to?(:uniq) ? instance.error_message.uniq.join(',') : instance.error_message }</span>
</div>
}
elsif html_tag =~ /<(input|textarea|select)/
html_tag
else
html_tag
end
end
error_messages 替代
最新推荐文章于 2021-03-17 14:00:49 发布
本文介绍了一种在Rails应用中自定义错误显示的方法。通过修改ActionView::Base.field_error_proc,可以实现更美观且统一的错误提示样式。此过程涉及HTML标签的匹配与替换,能够显著提升用户体验。
1232

被折叠的 条评论
为什么被折叠?



