In the previous essay, we completed the deletion of a table product, you must have noticed that we have not added any validation, such as price, we can enter any character, without error prompts. This time, let's take a look at how to add the validation input feature.
1. Open the Product.rb file in the Depot\app\models directory and add code to it as follows:
Class Product < ActiveRecord::Base
Validates_presence_of:title,:d escription,: Image_url
End
2. Open Http://127.0.0.1:3000/Admin/New again, leave title,description,image_url not input, directly click the Create button, will display error page, as follows:
3. The above added is required entry verification, the following add price item must be a number of validation.
To add code to the PRODUCT.RB:
Validates_numericality_of:p Rice
After adding the code, the PRODUCT.RB code is as follows:
Class Product < ActiveRecord::Base
Validates_presence_of:title,:d escription,: Image_url
Validates_numer icality_of:p Rice End