[RUBY] Get the error message using the any? method

I would like to summarize the methods used to display the error message.

What is the any? method

The any? method returns false if all elements are false. Immediately returns true if any element is true.

How to write is like this

p [false, nil].any? # => false

For example, suppose you write an error message description in a partial template and set it to be displayed only when the object has error information.


def create
    @item = Item.new(item_params)
    if @item.save
      redirect_to root_path
      render :new

When saving fails due to validation etc. with this description Set to return to the new action.


  <%= form_with model: @item, local: true do |f| %>
      <%= render 'shared/error_messages', model: @item %> 

Bring the model object with the error information to the render destination.


<% if model.errors.any? %>
<div class="error-alert">
    <% model.errors.full_messages.each do |message| %>
    <li class='error-message'><%= message %></li>
    <% end %>
<% end %>

Check the contents of errors with the any? method, and if it exists, it becomes true and the error is repeated.

Also quite similar to the present? method, but the any? method

In the above example, the error message is displayed by iterative processing.

If you want to display an error message for each label location, you can use the include? Method.

Recommended Posts

Get the error message using the any? method
[Rails] What was the error message?
Java comparison using the compareTo () method
[Rails 6] destroy using the resources method
[Ruby] Obtaining even values ​​using the even? Method
About the error message Invalid redeclaration of'***'
What to do if you get the error message unrecognized selector send to instance "***"
[Rails] Let's translate the error message into Japanese
The devise error message is not displayed properly.
About the method
[Rails] How to solve the error "undefined method` visit'" when using Capybara with Rspec
[Ruby] Cut out a string using the slice method
I get Mysql2 :: Error :: ConnectionError in the production environment
Examine the elements in the array using the [Ruby] includes? Method
I want to call the main method using reflection
Are you using the default method of the interface properly?
Why the get method is needed in the Calendar class
About No Method Error
[Ruby] Get in the habit of using the dup method when making a copy of a string variable
Flash message using devise
Error when using SnapKit
About the length method
About the authenticate method.
Training using each_with_index method
About the map method
About the ancestors method
About the to_s method.
Try using each_with_index method
[Note] Error message when using Rails Form object pattern Japanese
How to get the class name / method name running in Java
[Ruby] Count an even number in an array using the even? Method
[Rails] I tried using the button_to method for the first time
[Rails] I implemented the validation error message by asynchronous communication!
Get error information using DefaultErrorAttributes and ErrorAttributeOptions in Spring Boot 2.3
[Rails] Japanese localization of error message when using Form object