This time, I will summarize the validation that was packed in implementing Rails test code.
Generally, it is an English word used to judge the validity of verification, verification, etc. In IT, it is used to verify whether grammar etc. are written properly.
:presence Used to verify that it is not empty.
validates :item presence: true
By doing this, if you post a new item, you will not be able to post it if it is left blank.
In the case of: presence, ["○○ can`t be blank"] is set by default.
numericality It is often used when the column type is set to integer. Use by specifying only numerical values.
validates :price, numericality: true
The presence and usage are mainly the same.
length Validation that can limit the length
validate :price, presence: true, length: {maximum: 7}
This will limit the length. Often used with options such as maximum and minimum.
There are still more, but I used these three this time
Rails documentation (https://railsdoc.com/validation)
This will be my first post. I want to continue.
Recommended Posts