[RUBY] [Rails] What was the error message?

How to generate and display error messages



>> user = User.new

>> user.save    #Error message generation
=> false

>> user.error.full_messages  
=> [Failed"", "invalid"]

When saving an object, an error message is generated as an array element the moment false is returned.



<% @user.errors.full_messages.each do |msg| %>
  <%= msg %>
<% end %>

Each element of the array can be retrieved and displayed in each statement.

Class attribute that appears only on error

class="field_with_errors" It automatically encloses the input parts of the error part.


While using partials, it is implemented so that it can handle any object.


<% if object.errors.any? %>
  <div id="error_explanation">
    <div class="alert alert-danger alert-form-extend" role="alert">
      <%= object.errors.count %>There are an error
      <% @user.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
<% end %>


<%= form_with(model: @user, url: users_path, local: true) do |form| %>
  <%= render 'shared/error_messages', object: form.object %>


def create
  @user = User.new(user_params)
  if @user.save
    render 'new'


#error_explanation {
  color: red;
  ul {
    color: red;
    padding-bottom: 0;

.field_with_errors .form-control { 
  border-color: red;

Recommended Posts

[Rails] What was the error message?
[Rails] Let's translate the error message into Japanese
Rails ~ Understanding the message function ~
[Rails] I implemented the validation error message by asynchronous communication!
What I was interested in in Progate's Ruby on Rails course [Each statement of error message]
[Rails] What to do if you can't get an error message with the errors method
About the error message Invalid redeclaration of'***'
Get the error message using the any? method
Rails Flash Message
The road to Japaneseizing Rails devise error messages
The devise error message is not displayed properly.
[Rails] Japanese localization of validation error message ~ ja.yml ~
What to do if you get the error message unrecognized selector send to instance "***"
[Ruby on Rails] How to Japaneseize the error message of Form object (ActiveModel)
[Note] Rails error list
What to do if the Rails server can't start
Catch Rails Routing Error
[Ruby on Rails Tutorial] Error in the test in Chapter 3
[Rails error] unexpected tIDENTIFIER
Mac Rails Install Error
[Rails] What is the difference between redirect and render?
What should I use for the testing framework [Rails]
rails heroku error log
rails error resolution summary
[Rails] What are params?
Master the [Rails] scope!
What to do if you don't see the test code error message in the terminal console
[Rails] What to do when the view collapses when a message is displayed with the errors method
[Note] Error message when using Rails Form object pattern Japanese
The story when the test folder was not created in Rails
What I was addicted to when introducing the JNI library
[Rails] What is the difference between bundle install and bundle update?
What I was addicted to while using rspec on rails
I entered from Rails and didn't know what [attr_accessor] was
From Ruby on Rails error message display to Japanese localization
What I was addicted to with the Redmine REST API
[Rails] Japanese localization of error message when using Form object
What to do if you can't use the rails command
[Memorandum] What is an error?
[Rails] What to do when the error No database selected and Unknown database appears in db: migrate
I knew what reflection was
What is the pluck method?
What is Rails gem devise?
rails error Library not loaded
about the where method (rails)
[rails] What are Strong Parameters?
[Programming beginner] What to do when rails s becomes an error in the local development environment
What is the BufferedReader class?
[Environment variables] What are rails environment variables?
Error in rails db: migrate
[Rails] Rails version upgrade error memorandum
Error when using rails capybara
What is the constructor for?
With the error Cordova-> Looper.prepare ()
[rails] error during devise installation
[Rails] Delete the migration file
What is Rails Active Record?
What is the initialize method?
I tried to summarize what was asked at the site-java edition-
What to do if the Rails page doesn't appear in Rails tutorial 1.3.2
I got a warning message with the rails _6.0.3_ new hello_myapp command