Ruby on Rails development from scratch (Windows) (v)-Validate input

Source: Internet
Author: User
Tags ruby on rails

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.