Display error message
■ UX improvement ■ Improved understanding of partial templates
■ Mac OS catalina ■ Ruby on Rails (5.2.4.2) ■ Virtual Box:6.1 ■ Vagrant: 2.2.7
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
models/item.rb
class Item < ApplicationRecord
validates :name, presence: true
validates :text, presence: true
validates :amount, presence: true
end
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 %>
The original scaffold
Partial
that's all. as you like! !! !!
■ 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