[Ruby on Rails] Individual display of error messages

Target

error.gif

Development environment

ruby 2.5.7 Rails 5.2.4.3 OS: macOS Catalina

Premise

-Build login environment with devise

edit view

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" %>

Supplement

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

[Ruby on Rails] Individual display of error messages
[Ruby on Rails] How to display error messages
[Rails] Japanese localization of error messages
Basic knowledge of Ruby on Rails
[Ruby on Rails] Add and delete tags and display (success / error) messages using ajax.
Animated display of Swift error messages
(Ruby on Rails6) Display of the database that got the id of the database
From Ruby on Rails error message display to Japanese localization
[Ruby on Rails] Introduction of initial data
[Rails] Addition of Ruby On Rails comment function
Let's summarize "MVC" of Ruby on Rails
part of the syntax of ruby ​​on rails
[Rails] How to display error messages individually
[Ruby on Rails] Japanese notation of errors
Explanation of Ruby on rails for beginners ①
[Ruby on rails] Implementation of like function
Implementation of Ruby on Rails login function (Session)
About Ruby error messages
[Ruby on Rails] Until the introduction of RSpec
Recommendation of Service class in Ruby on Rails
Ruby on Rails ~ Basics of MVC and Router ~
Ruby on Rails Elementary
[Ruby on Rails] A memorandum of layout templates
Ruby on Rails basics
Ruby On Rails Association
[Ruby on Rails] Ranking display (total, average value)
[Ruby on Rails] How to Japaneseize the error message of Form object (ActiveModel)
Ruby on Rails <2021> Implementation of simple login function (form_with)
[Ruby on Rails] Asynchronous communication of posting function, ajax
[Ruby on Rails Tutorial] Error in the test in Chapter 3
Implementation of Ruby on Rails login function (devise edition)
Docker the development environment of Ruby on Rails project
Japaneseization of Rails error messages [devise / Form objects, etc.]
[Ruby on Rails] Posting score ranking function (whole display)
[Ruby on Rails] Implementation of tagging function/tag filtering function
Explanation of Ruby on rails for beginners ⑥ ~ Creation of validation ~
Explanation of Ruby on rails for beginners ② ~ Creating links ~
Try using the query attribute of Ruby on Rails
Explanation of Ruby on rails for beginners ⑦ ~ Flash implementation ~
Japanese localization of error messages
Ruby on rails learning record -2020.10.03
Portfolio creation Ruby on Rails
Ruby on rails learning record -2020.10.04
[Ruby on Rails] Debug (binding.pry)
Ruby on rails learning record -2020.10.05
Ruby on rails learning record -2020.10.09
Ruby on Rails config configuration
Ruby on Rails basic learning ①
Ruby on rails learning record-2020.10.07 ②
Display Flash messages in Rails
Ruby on rails learning record-2020.10.07 ①
Ruby on rails learning record -2020.10.06
Ruby on Rails validation summary
[Ruby on Rails] undefined method ʻid'for nil: NilClass error resolution method
[Ruby on Rails] Quickly display the page title in the browser
[Procedure 1 for beginners] Ruby on Rails: Construction of development environment
[Ruby on Rails] Elimination of Fat Controller-First, logic to model-
Delete all the contents of the list page [Ruby on Rails]
A note about the seed function of Ruby on Rails
[Ruby on Rails] Easy scroll animation of javascript (using ScrollReveal.js)
How to display a graph in Ruby on Rails (LazyHighChart)