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:
- No validation of Added field contents in any form when added
- 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.
- First, the user name and the contents of the mailbox cannot be empty
- The user name must be a string, and the length cannot be too long, typically 20 characters
- Mailbox format conforms to basic Mailbox format specification
- 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.
case_sensitive就是大小写敏感,为false表示不区分大小写即不敏感
?
?
?
?
?
?
?
10--rails Data Interaction 2