[RUBY] [Rails] How to get success and error messages


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

How to send a success message


   flash[:notice] = "successfully" 

↑ is described in the create action and update action of the controller.


   <% if flash[:notice] %>
     <p id="notice">
       <%= flash[:notice] %>
   <% 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".

How to get an error message


<% if @book.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@book.errors.count, "error") %> prohibited this book from being saved:</h2>
        <% @book.errors.full_messages.each do |message| %>
          <li><%= message %></li>
        <% end %>
<% 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

[Rails] How to get success and error messages
[Rails] How to display error messages individually
[Ruby on Rails] How to display error messages
How to display error messages and success messages when registering as a user
[Rails] How to use flash messages
How to get along with Rails
[Rails] How to display error messages for comment function (for beginners)
How to display error messages in Japanese
[Rails] How to get the URL of the transition source and redirect
The road to Japaneseizing Rails devise error messages
How to write Rails
[Java] How to get and output standard input
How to get and study java SE8 Gold
How to build API with GraphQL and Rails
[Rails] How to solve "Uglifier :: Error: Unexpected character'`'"
Rails scope anti-patterns and how to eliminate them
[Rails] How to get rid of flash messages in a certain amount of time
Rails / Ruby: How to get HTML text for Mail
[Rails] validates How to translate error sentences into Japanese
[Rails] How to get the contents of strong parameters
[Ruby] How to get the tens place and the ones place
(Ruby on Rails6) How to create models and tables
How to get started with creating a Rails app
[Kotlin] How to get IP address and user agent
[RSpec] How to test error messages set by Shoulda-Matchers
[Rails] How to use enum
How to read rails routes
How to use rails join
How to terminate rails server
How to write Rails validation
How to write Rails seed
[Rails] How to disable turbolinks
[Rails] How to use authenticate_user!
[Rails] How to implement scraping
[Rails] How to make seed
How to write Rails routing
[Rails] How to install simple_calendar
[Rails] How to install reCAPTCHA
[Rails] How to use Scope
[Rails] How to define macros in Rspec and standardize processing
[Rails] Differences between redirect_to and render methods and how to output render methods
How to set and describe environment variables using Rails zsh
How to batch run JUnit and get coverage as well
[Rails] How to use gem "devise"
[Rails] Japanese localization of error messages
How to use StringBurrer and Arrays.toString.
How to use Java HttpClient (Get)
How to deploy jQuery on Rails
[Rails] How to install Font Awesome
[Rails] How to use devise (Note)
How to get started with slim
[rails] How to display db information
How to use EventBus3 and ThreadMode
[Rails] How to prevent screen transition
How to use Ruby on Rails
How to deploy Bootstrap on Rails
[Rails] Display form error messages asynchronously
How to call classes and methods
[Rails] How to speed up docker-compose
How to use equality and equality (how to use equals)
[Rails] How to add new pages