10--rails Data Interaction 2

Source: Internet
Author: User

Model class is ActiveRecord, through the last lesson we can use the Micro-blog app to add users, such as Sundi, but there are two major problems:

    1. No validation of Added field contents in any form when added
    2. No visual interface makes it easy for users to add new users to the area, we use the Rails console

In this lesson, we set out to solve the first problem, which is the data validation problem of model class ActiveRecord in rails.

Let's get into the rails console first.

This time we copied the last lesson to create an object again, but this time we're not adding parameters after new.

Let's take a look at this and see that the new user's username and mailbox are empty:

With this example you can see that because there is no validation, creating a user does not have any form of validation, so it is obviously unreasonable for you to create a completely useless empty user. So we need the rails validation mechanism, and we don't create and give hints when we type in the wrong format.

    1. First, the user name and the contents of the mailbox cannot be empty
    2. The user name must be a string, and the length cannot be too long, typically 20 characters
    3. Mailbox format conforms to basic Mailbox format specification
    4. Finally we want to use the mailbox as the user's unique identity, different users can not have the same mailbox

As above, the user model requires four types of validation: existence verification, degree validation, format validation, uniqueness verification

The key word for data validation is validates, and the existence of validation has its own parameters presence

Existence validation is checking that the corresponding field property is empty

The use is to open the model file, as follows:

After editing is:

Add Data validation keyword validates (must be separated from the first colon with a space), followed by the field to be validated, followed by the method to be verified is called presence:true means must exist

It may be difficult to understand the above, in fact, Ruby language is this way, the method of parentheses can not be written, validates is the method, followed by parameters, the following parentheses can also:

After adding this part of the code, rails validates the username field when we create a new user.

Let's get out of here. Then console re-enter:

After you add the data validation, the new method does not error, but you can see that the Save method returns False or failed

We look at the relevant error message:

The error message is that username cannot be empty, which means that data validation is in effect

We can also add validation to the mailbox,

Here we have completed the existence of verification, the following completion of the length of verification, directly in the back of the length of the validation parameters of the parameter length using the following

Note that legth is used after curly braces because the length limit has a maximum minimum limit, and so on more than one limit, multiple parameters are enclosed in curly braces. Here just set the maximum length of 20 characters.

Let's just exit and then re-enter the console:

Same with length verification for mailboxes

Next we'll add format validation and uniqueness validation to the mailbox

Format validation specific parameter is format, curly brace with a regular expression followed by a

Let's just quit and re-enter the console to experiment:

Invalid translation is not legal.

Next is the uniqueness verification, the unique parameter is uniqueness

Too long, let's add a comma and wrap the input.

But rails defaults to verifying that the case is different, that is, uppercase and lowercase mailboxes, rails will think of two different things, which is certainly not possible. Therefore, uniqueness validation is required to add upper case insensitivity.









10--rails Data Interaction 2

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.