我正在尝试Rails4中的确认验证,但它无法正常工作。 当我提交表单时,邮件是"电子邮件确认不能为空白"而不是"电子邮件不匹配确认" 这是我的代码:
enter code here
#model user.rb
class User < ActiveRecord::Base
validates :email, confirmation: true
validates :email_confirmation, presence: true
end
#view
<div class="field">
<%= f.label :email_confirmation %><br>
<%= f.text_field :email_confirmation %>
</div>
#controller
def create
@user = User.new(user_params)
respond_to do |format|
if @user.save
format.html { redirect_to @user, notice: 'User was successfully created.' }
format.json { render :show, status: :created, location: @user }
else
format.html { render :new }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end
答案 0 :(得分:0)
您可以创建自定义验证以检查两个字段是否相同
validate :check_email
def check_email
errors.add(:email_comfirmation, "Test message") if email == email_confirmation
end