已执行验证但字段未在 Rails 3.2.0 上用 field_with_errors div 包围,仅在生产中 [英] Validations performed but fields not surrounded with field_with_errors div on Rails 3.2.0 only in production
问题描述
我有一个发布模型,其中包含对某些字段(例如标题)的验证.
I have a Publication model with validations on some fields, title for example.
我删除了我觉得烦人的错误消息,并为当输入被 field_with_error div 包围时设置了一个漂亮的 CSS,以便用户知道哪个字段没有验证.
I removed the error messages that I find annoying, and set up a nice CSS for when the input are surrounded in a field_with_error div so the user knows what field did not validate.
问题是当我部署到生产环境时,仍然会执行验证(即用户被发送回表单),但输入没有被错误 div 包围.
The thing is when I deploy to production the validations are still performed (ie. the user is sent back to the form) but the inputs are not surrounded with the error div.
我尝试在生产模式下在本地运行该应用程序,我所能了解到的是,当我在 config/environments/production.rb 文件中将 config.cache_classes 设置为 true 时,它开始发生.
I tried running the app locally in production mode and all I could learn is that it starts happening when I set config.cache_classes to true in my config/environments/production.rb file.
此外,当我在控制器中记录 @publication.errors 时,错误也会出现.
Also when I log @publication.errors in the controller the errors are present.
有什么想法吗?
推荐答案
在 rails 4.0.1 项目中,我添加了一个初始化程序:
On a rails 4.0.1 project I added an initializer:
config/initializers/field_with_errors.rb
用这些行
ActionView::Base.class_eval do
@@field_error_proc = Proc.new{ |html_tag, instance| "<div class=\"field_with_errors\">#{html_tag}</div>".html_safe }
end
这篇关于已执行验证但字段未在 Rails 3.2.0 上用 field_with_errors div 包围,仅在生产中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!