Rails 3:“有错误的字段"wrapper 改变页面外观.如何避免这种情况? [英] Rails 3: "field-with-errors" wrapper changes the page appearance. How to avoid this?
本文介绍了Rails 3:“有错误的字段"wrapper 改变页面外观.如何避免这种情况?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
电子邮件字段:
<label for="job_client_email">Email: </label>
<input type="email" name="job[client_email]" id="job_client_email">
看起来像这样:
但是,如果邮件验证失败,就会变成:
But, if the email validation fails, it becomes:
<div class="field_with_errors">
<label for="job_client_email">Email: </label>
</div>
<div class="field_with_errors">
<input type="email" value="wrong email" name="job[client_email]" id="job_client_email">
</div>
看起来像这样:
如何避免这种外观变化?
How could I avoid this appearance change ?
推荐答案
你应该重写 ActionView::Base.field_error_proc
.它目前在 ActionView::Base
:
You should override ActionView::Base.field_error_proc
. It's currently defined as this within ActionView::Base
:
@@field_error_proc = Proc.new{ |html_tag, instance|
"<div class="field_with_errors">#{html_tag}</div>".html_safe
}
您可以通过将它放在 config/application.rb
内的应用程序类中来覆盖它:
You can override it by putting this in your application's class inside config/application.rb
:
config.action_view.field_error_proc = Proc.new { |html_tag, instance|
html_tag
}
重启 Rails 服务器以使此更改生效.
Restart rails server for this change to take effect.
这篇关于Rails 3:“有错误的字段"wrapper 改变页面外观.如何避免这种情况?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文