[Rails] What to do if you can't get an error message with the errors method

Introduction

For example, when trying to save the variable result like result.save, if it fails, an error message will be stored in result. I want to use this to display an error message, but it becomes a Ruby error screen.

In other words, I don't get the error message I want.

Check on the console when an error occurs


result.errors.any? => False

I can't save it, but I don't get an error.

This time I want to get an error when trying to register without input.

Cause

The cause was that there was no validation description in Model. When creating a table, there are restrictions on columns (null: false because of no input restrictions), but that alone does not result in an error message.

Countermeasures

I wrote the following in Model.


class Result < ApplicationRecord
  belongs_to ...
  has_many ...
  validates :name, presence: true
  
  ...
end

By writing validates: name, presence: true


result.errors.any? => True

Next door


result.errors.full_messages

So, I got the contents of all the error messages and retrieved them using ʻeach`!

Recommended Posts

[Rails] What to do if you can't get an error message with the errors method
[Rails] What to do if you can't get parameters with form_with
What to do if you get an error during rails db: reset
What to do if you get the error message unrecognized selector send to instance "***"
What to do if you can't use the rails command
What to do if you can't get the text of an element in Selenium
What to do if you get the error Couldn't find Item without an ID
What to do if you get an "A server is already running." Error when you try to start the rails server
[Rails] What to do when the view collapses when a message is displayed with the errors method
What to do if you get an error in Basic authentication during Rails test code
What to do if you get an uninitialized constant Likes Controller error
What to do if you get an error when you hit Heroku logs
What to do if you get an error saying "Please enter a valid value" when getting with Rails datetime_field
What to do if the Rails server can't start
What to do if you get an error on heroku rake db: migrate
What to do if you get an error with bundle install by entering gem'bcrypt' in your Gemfile
What to do if you should have installed Rails but an error occurs with rails -v (for beginners)
What to do if you get the error Too long with no output (exceeded 10m0s) on CircleCI
What to do when you launch an application with rails
What to do if you get angry with OpenSSL with pyenv install
What to do if you get a "302" error in your controller unit test code in Rails
What to do if you get an error saying "Could not find a JavaScript runtime." When starting rails server
[Rails] What to do if you get an error saying "Could not find a JavaScript runtime." When executing the rails s command on Catalina
What to do if you get a "Mysql2 :: Error: Operand should contain 1 column (s)" error in Rails
What to do if you get an [An HTTP request took too long to complete.] Error in Docker.
What to do if you get a SQLite3 :: BusyException: database is locked error
What to do if an ActionController :: UnknownFormat error occurs
What to do if you get To install the missing version, run `gem install bundler: 2.1.4`
What to do if you can't bundle update and bundle install after installing Ruby 3.0.0 in the Rails tutorial
How to translate the error message into Japanese (What to do if you cannot log in for some reason)
After installing'devise''bootstrap' of gemfile with rails, what to do when url is an error
What to do if you get a wrong number of arguments error in binding.pry
What to do if you can't find your API key after deploying to Rails heroku
What to do if you get a port error when docker-compose up on Mac
What to do if you cannot execute with the command "Java package name / class name"
# What to do if you accidentally do rails db: migrate: drop
[Rails] What was the error message?
[Java] What to do if you get an error in Eclipse saying "Not allowed at source level below 1.X"
What to do if you get an Argument Error: wrong number of arguments (given 2, expected 0) in your RSpec test
What to do if you get the warning "Uniqueness validator will no longer enforce case sensitive comparison in Rails 6.1." in Rails 6.0
[Rails] What to do if you accidentally install bundle in the production environment in your local environment
What to do if you change the Listen Address from the settings screen with Mattermost docker
What to do if you installed Ruby with rbenv but the version does not change
What to do if you get a "Cannot Pull Container Error" when starting ECS ​​Fargate
What to do if validation doesn't work with the update action
What to do if Cloud9 is full in the Rails tutorial
What to do if you forget the root password in CentOS7
What to do if you cannot roll back the migration (UnknownMigrationVersionError)
<Dot installation> What to do if you cannot proceed due to an error when building a development environment for Rails learning.
Get the error message using the any? method
What to do if you install Ubuntu
[Programming beginner] What to do when rails s becomes an error in the local development environment
What to do if an SSH key authentication error occurs during automatic deployment to EC2 with Capistrano
What to do if you get a JNI shared library error when trying to build in Eclipse
What to do if the app is not created with the latest Rails version installed when rails new
What to do if you get a groovy warning in Thymeleaf Layout
Add gem'rails-i18n','~> 6.0.0' and what to do if bundle install gives an error
What to do if an error occurs when nokogiri enters when bundle install
no space left on device What to do if an error occurs
What to do if you don't like the code generated by swagger-codegen-cli
What to do if you get a MiniMagick vulnerability alert on GitHub