ruby 2.5.7 Rails 5.2.4.3 OS: macOS Catalina
-Build login environment with devise
In the initial state, it is described as follows.
erb:app/views/users/registrations/new.html.erb
<h2>Sign up</h2>
<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<%= render "users/shared/error_messages", resource: resource %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name, autofocus: true, autocomplete: "name" %>
</div>
<div class="field">
<%= f.label :email %><br />
<%= f.email_field :email, autofocus: true, autocomplete: "email" %>
</div>
<div class="field">
<%= f.label :password %>
<% if @minimum_password_length %>
<em>(<%= @minimum_password_length %> characters minimum)</em>
<% end %><br />
<%= f.password_field :password, autocomplete: "new-password" %>
</div>
<div class="field">
<%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation, autocomplete: "new-password" %>
</div>
<div class="actions">
<%= f.submit "Sign up" %>
</div>
<% end %>
<%= render "users/shared/links" %>
Make edits to the above.
erb:app/views/users/registrations/new.html.erb
<h2>Sign up</h2>
<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<% if @user.errors.any? %>
<% end %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name, autofocus: true, autocomplete: "name" %>
<% if @user.errors.include?(:name) %>
<p style="color: red;"><%= @user.errors.full_messages_for(:name).first %>
<% end %>
</div>
<div class="field">
<%= f.label :email %><br />
<%= f.email_field :email, autofocus: true, autocomplete: "email" %>
<% if @user.errors.include?(:email) %>
<p style="color: red;"><%= @user.errors.full_messages_for(:email).first %>
<% end %>
</div>
<div class="field">
<%= f.label :password %>
<% if @minimum_password_length %>
<em>(<%= @minimum_password_length %> characters minimum)</em>
<% end %><br />
<%= f.password_field :password, autocomplete: "new-password" %>
<% if @user.errors.include?(:password) %>
<p style="color: red;"><%= @user.errors.full_messages_for(:password).first %>
<% end %>
</div>
<div class="field">
<%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation, autocomplete: "new-password" %>
<% if @user.errors.include?(:password_confirmation) %>
<p style="color: red;"><%= @user.errors.full_messages_for(:password_confirmation).first %>
<% end %>
</div>
<div class="actions">
<%= f.submit "Sign up" %>
</div>
<% end %>
<%= render "users/shared/links" %>
python
(1) Delete the following description and add an if statement.@Check for user errors.
<%= form_for〜%>
<%= render "users/shared/error_messages", resource: resource %>
↓
<%= form_for〜%>
<% if @user.errors.any? %>
<% end %>
(2) Add the following column names under each field after editing.
<% if @user.errors.include?(:name) %>
<p style="color: red;"><%= @user.errors.full_messages_for(:name).first %>
<% end %>
If you want to add validation and see more errors, If you add as below, it's OK. There are many types of validation, so please check it out. By the way, I have confirmed that the following validations are not blank.
app/models/user.rb
validates :name, presence: true
Recommended Posts