【问题标题】:Rails validation- confirmation not correctRails 验证 - 确认不正确
【发布时间】:2016-06-17 13:36:08
【问题描述】:

我正在尝试在 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

【问题讨论】:

  • email 的字段在哪里?我在这里只能看到email_confirmation...并且存在验证将给出“不能为空白”的消息。

标签: ruby-on-rails validation


【解决方案1】:

您可以创建自定义验证来检查两个字段是否相同

validate :check_email

def check_email
  errors.add(:email_comfirmation, "Test message") if email == email_confirmation
end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-08
    • 2017-01-19
    • 2015-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-26
    相关资源
    最近更新 更多