Rails 4 validates_confirmation_of未触发

时间:2015-09-22 20:10:04

标签: ruby-on-rails ruby ruby-on-rails-4

我知道我必须错过一些简单的事情。我有一个包含validate_confirmation_of的模型。我知道它在我的模型中有效(我已经使用控制台对其进行了测试)但是出于某种原因,似乎表单上的确认字段没有正确链接到我的模型中,因此模型认为确认字段是空白的,因此跳过验证。

模型

class Person < ActiveRecord::Base
  enum status: { suspended: 0, pending_validation: 1, pending_setup: 2, registered: 3, unknown: 99 }

  EMAIL_REGEX = /\A[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}\Z/i

  validates :first_name, :presence => true, :length => { :maximum => 255 }
  validates :last_name, :presence => true, :length => { :maximum => 255 }
  validates :email_address, :presence => true, :length => { :maximum => 255 }, :uniqueness => true, :format => { :with => EMAIL_REGEX }, :confirmation => true
  validates :country_code, :presence => true

end

查看

<%= form_for @person, url: { action: 'create' } do |f| %>

  <fieldset class="registration">

    <legend>Your Information</legend>

      <div class="group column-1">
        <%= label_tag('First Name') %> <span class="required">*</span>
        <%= f.text_field :first_name, class: 'form-control', :tabindex => 1 %>

        <%= label_tag('Display Name') %>
        <%= f.text_field :display_name, class: 'form-control', :tabindex => 3 %>
      </div>

      <div class="group column-2">
        <%= label_tag('Last Name') %><span class="required">*</span>
        <%= f.text_field :last_name, class: 'form-control', :tabindex => 2 %>

        <%= label_tag('Country') %> <span class="required">*</span>
        <%= f.collection_select :country_code, Country.order(:name), :iso_code, :name, { :include_blank => true }, { :class => 'form-control', :tabindex => 4 } %>

      </div>

      <div class="column-full">
        <%= label_tag('Email Address') %> <span class="required">*</span>
        <%= f.text_field :email_address, class: 'form-control', :tabindex => 5 %>

        <%= label_tag('Confirm Email Address') %> <span class="required">*</span>
        <%= f.text_field :email_address_confirmation, class: 'form-control', :tabindex => 6 %>
      </div>

      <div class="checkbox">
        <label>
            <%= check_box_tag 'chkTerms'  %>
            I accept the <%= link_to 'Terms & Conditions', '/pages/terms' %> <span class="required">*</span>
        </label>
      </div>

      <div class="button-options">

        <%= f.submit 'Submit', class: 'btn btn-danger' %>
        <%= link_to 'Cancel', '/pages/home', class: 'link-button-cancel' %>
      </div>
    </fieldset>
<% end %>

控制器

def create
    @person = Person.new(register_params)
    @person.status = 'pending_validation'

    if @person.save
       render('submit')
    else
       render('register')
    end

end

private
    def register_params
      params.require(:person).permit(:first_name, :last_name, :email_address, :display_name, :country_code)
    end

如您所见,在我看来,我有一个“email_address_confirmation”text_field。我创建它的方式是我创建所有其他字段,它们都工作正常,所以对于我的生活,我无法弄清楚代码有什么问题。但是当我提交页面时,无论我在确认字段中输入什么内容,确认验证都不会触发。

如果重要,我使用的是Rails 4.2.0和Ruby 2.1.5p273。

1 个答案:

答案 0 :(得分:0)

您需要在模型中添加:

validates :email_address_confirmation, presence: true

根据Rails guides

  

仅当email_confirmation不是nil时才会执行此(确认)检查。

同样在控制器的方法register_params中进行更改,错过许可参数中的email_address_confirmation字段:

def register_params
  params.require(:person).permit(:first_name, :last_name, :email_address, :email_address_confirmation, :display_name, :country_code)
end