samedi 31 août 2019

How to validate emails with error messages

I am trying to validate the format of users email.

I have tried this

<% if @user.errors.any? %>
<h2> Error! Invalid password or Email </h2>
<ul>
<% @users.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
<% end %> 

I experimented with this from a old ruby video but it didnt seen to pan out. this is my user.rb model

class User < ApplicationRecord
        has_secure_password
        validates :email, presence: true, uniqueness: true
        validates :password, length: { minimum: 4 }
        validates :email, format: { with: /(\A([a-z]*\s*)*\<*([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\>*\Z)/i }
end

I get this error currently

No route matches {:action=>"show", :controller=>"users", :id=>nil}, missing required keys: [:id] undefined method `errors' for nil:NilClass whenever the password or email makes an error from the requirements.

Aucun commentaire:

Enregistrer un commentaire