I tried to summarize how to put out success messages and error messages when posting with an application like twitter. It was a hassle for me to add this function for the first time, so I will post it for people who have similar problems.
ruby 2.5.7
Rails 5.2.4.3
controller
flash[:notice] = "successfully"
↑ is described in the create action and update action of the controller.
View
<% if flash[:notice] %>
<p id="notice">
<%= flash[:notice] %>
</p>
<% end %>
Write ↑ on the page of the view where you want to send a success message. Now, when you can post, you should see the word "successfully".
View
<% if @book.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@book.errors.count, "error") %> prohibited this book from being saved:</h2>
<ul>
<% @book.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
Write ↑ on the page of the view where you want to issue an error message. That way, you'll get an error message when you can't post because of validation.
If you want to know more, please refer to here.
Recommended Posts