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.
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.
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`!