2016-06-17 6 views
-1

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

여기서'email' 필드는 어디에 있습니까? 여기서'email_confirmation' 만 볼 수 있습니다 ... 그리고 존재 확인은 "can not be blank"메시지를줍니다. –

+0

<%= f.label :email %>
<%= f.text_field :email %>

답변

0

당신이 있는지 확인하는 사용자 정의 유효성 검사를 만들 수 있습니다라는 메시지가 양식을 제출하면 다음 "을 확인 일치하지 않는 이메일이"내 코드 대신에 "이메일 확인은 비워 둘 수 없습니다" 두 필드가 동일합니다.

validate :check_email 

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