[ERROR message display] A simplified version that can be used at any time with the rails partial template.

【goal】

Display error message

画面収録 2020-06-23 18.49.15.mov.gif

【merit】

■ UX improvement ■ Improved understanding of partial templates

【Development environment】

■ Mac OS catalina ■ Ruby on Rails (5.2.4.2) ■ Virtual Box:6.1 ■ Vagrant: 2.2.7

【Implementation】

Application creation

mac.terminal


$ rails new error_test
$ cd error_test
$ rails g scaffold Item name:string texte:text amount:integer
$ rails db:migrate
$ cd app/views/layouts
$ touch _error_messages.html.erb

Model postscript

models/item.rb


class Item < ApplicationRecord
  validates :name, presence: true
  validates :text, presence: true
  validates :amount, presence: true
end

View postscript

layputs/_error_messages.html.erb


<% if model.errors.any? %>
  <div class="alert alert-warning">
    <ul>
      <% model.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
    </ul>
  </div>
<% end %>

※to add

Items/_form.html.erb


<%= render 'layouts/error_messages', model: f.object %>

/_form.....


<% if item.errors.any? %>
<div id="error_explanation">
  <h2><%= pluralize(item.errors.count, "error") %> prohibited this item from being saved:</h2>

  <ul>
    <% item.errors.full_messages.each do |message| %>
    <li><%= message %></li>
    <% end %>
  </ul>
</div>
<% end %>

that's all. as you like! !! !!

[I want to read it together]

■ Regarding error messages https://qiita.com/ryuuuuuuuuuu/items/1a1e53d062bff774d88a

■hidden_field https://qiita.com/tanaka-yu3/items/0d454c5ef80f8267f09d

■ Application creation https://qiita.com/tanaka-yu3/items/3fe1ed2852c6513d3583

Recommended Posts

[ERROR message display] A simplified version that can be used at any time with the rails partial template.
Power skills that can be used quickly at any time --Reflection
[Rails] "pry-rails" that can be used when saving with the create method
Create a page control that can be used with RecyclerView
About the problem that the server can not be started with rails s
Ruby array methods that can be used with Rails (other than each)
Organize methods that can be used with StringUtils
[Ruby] Methods that can be used with strings
A ruby ​​script that creates an rsa private key that can be used with OpenSSL from any two prime numbers
About the matter that hidden_field can be used insanely
Ruby on Rails 5 quick learning practice guide that can be used in the field Summary
Learning Ruby with AtCoder Beginners Selection [Some Sums] Increase the methods that can be used
Summary of css selectors that can be used with Nookogiri
Firebase-Realtime Database on Android that can be used with copy
Four-in-a-row with gravity that can be played on the console
Static analysis tool that can be used on GitHub [Java version]
I got a warning message with the rails _6.0.3_ new hello_myapp command
I made a question that can be used for a technical interview
[Rails] About Uglifier :: Error: Unexpected token: at the time of deployment
SwiftUI View that can be used in combination with other frameworks
Lock_version may be used for tables that tend to access and edit the same record at the same time
[Spring Boot] List of validation rules that can be used in the property file for error messages