[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.
Implementation method of linking multiple images to one post and posting at the same time
Tools and commands that may be useful for Java troubleshooting
Power skills that can be used quickly at any time --Reflection
Try Alfresco 6.0 on Windows (Docker will be introduced at the same time)
[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
Ruby array methods that can be used with Rails (other than each)
I made a question that can be used for a technical interview
Power skills that can be used quickly at any time --Reflection
Organize methods that can be used with StringUtils
About the matter that hidden_field can be used insanely
Learning Ruby with AtCoder Beginners Selection [Some Sums] Increase the methods that can be used
I made a question that can be used for a technical interview
[Rails] About Uglifier :: Error: Unexpected token: at the time of deployment
[Spring Boot] List of validation rules that can be used in the property file for error messages