包装器更改页面外观

包装器更改页面外观

本文介绍了Rails 3:“有错误的字段"包装器更改页面外观.如何避免这种情况?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

电子邮件字段:

<label for="job_client_email">电子邮件:</label><input type="email" name="job[client_email]" id="job_client_email">

看起来像这样:

但是,如果电子邮件验证失败,则变为:

<label for="job_client_email">电子邮件:</label>

<div class="field_with_errors"><input type="email" value="wrong email" name="job[client_email]" id="job_client_email">

看起来像这样:

我怎样才能避免这种外观变化?

解决方案

您应该覆盖 ActionView::Base.field_error_proc.它目前在 ActionView::Base:

中定义为

 @@field_error_proc = Proc.new{ |html_tag, instance|"<div class=\"field_with_errors\">#{html_tag}</div>".html_safe}

您可以通过将其放在 config/application.rb 中的应用程序类中来覆盖它:

config.action_view.field_error_proc = Proc.new { |html_tag, instance|html_tag}

重新启动 rails 服务器以使此更改生效.

Email field:

<label for="job_client_email">Email: </label>
<input type="email" name="job[client_email]" id="job_client_email">

looks like this:

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>

which looks like this:

How could I avoid this appearance change ?

解决方案

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
 }

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
}

Restart rails server for this change to take effect.

这篇关于Rails 3:“有错误的字段"包装器更改页面外观.如何避免这种情况?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 23:34