Rails表单字段重复错误消息

时间:2012-02-26 20:31:10

标签: ruby-on-rails ruby-on-rails-3 validation

我在表单字段周围收到重复的错误消息 - 一个用于标签,另一个用于字段。我不知道为什么标签被分配了错误信息。我已经包含了我所知道的所有相关代码。如果可以,请帮忙,谢谢! rails version 3.0.11

生成的html:

<div class="field">
<div class="field_with_errors"><label for="customer_email">Email</label><br /><span class="validation-error">&nbsp;
      has already been taken</span></div><br />
<div class="field_with_errors"><input id="customer_email" name="customer[email]" size="30" type="text" value="asdrummo@gmail.com" /><br /><span class="validation-error">&nbsp;
      has already been taken</span></div>
</div>
       密码
       

在我的application.rb中:

config.action_view.field_error_proc = Proc.new do |html_tag, instance|
  if instance.error_message.kind_of?(Array)
    %(<div class="field_with_errors">#{html_tag}<br /><span class="validation-error">&nbsp;
      #{instance.error_message.join(',')}</span></div>).html_safe
  else
    %(<div class="field_with_errors">#{html_tag}<br /><span class="validation-error">&nbsp;
      #{instance.error_message}</span></div>).html_safe
  end
end

我的表单如下:

<%= form_for(@customer, :url => {:action => 'save_customer'}) do |f| %>

<% if @customer.errors.any? %>
<%= error_messages_for(@customer)%>
<% end %>

  <div class="field">
    <%= f.label :email %><br />
    <%= f.text_field :email %>
  </div>

  <div class="field">
    <%= f.label :password %><br />
    <%= f.password_field :password %>
  </div>

  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

error_messages_for(object):

def error_messages_for( object )
    render(:partial => 'shared/error_messages', :locals => {:object => object})
end

共享/ error_messages:

<div class="error">Please correct the <%= pluralize(@customer.errors.count, "error") %> below and resubmit.</div>

最后,我的验证:

validates :email, :presence => true, :length => {:maximum => 100 }, :format => EMAIL_REGEX, :confirmation => true, :uniqueness => true
validates :password, :on => :update_password, :presence => true, :confirmation => true, :length => { :minimum => 5}

更新

从application.rb中删除代码,我仍然得到以下内容(电子邮件验证错误示例):

<div class="field">
  <div class="field_with_errors"><label for="customer_email">Email</label></div><br />
  <div class="field_with_errors"><input id="customer_email" name="customer[email]" size="30" type="text" value="asdrummo@gmail.com" /></div>
</div>

0 个答案:

没有答案