Based on chapter 6, the task of this chapter is to make the application more robust-to ensure that errors in data are never committed to the database. Iteration B: verification target: 1. The field title, description, and image URL are not empty. 2. The price is a valid number, and no less than 0.013, titles are unique among all products 4. Images
Based on chapter 6, the task of this chapter is to make the application more robust-to ensure that errors in data are never committed to the database. Iteration B: verification target: 1. The field title, description, and image URL are not empty. 2. The price is a valid number, and no less than 0.01 3. Titles are unique among all products 4. Images
On the basis of chapter 6TaskIs to make the application more robust-to ensure that errors in data are never committed to the database.
Iteration B:Verify
Objectives:
1. The field title, description, and image URL are not empty.
2. The price is a valid number and should not be less than 0.01
3. The title is unique among all products.
4. The image URL looks valid.
The model layer is placedVerifyThe ideal position of the Code.
Modify the model layer file generated by the scaffold based on the above four targets.
Model file location:
The content of the initial file is as follows:
AddVerifyCode:
class Product < ActiveRecord::Base attr_accessible :description, :image_url, :price, :title validates :title, :description, :image_url, :presence => true validates :price, :numericality => {:greater_than_or_equal_to => 0.01} validates:title, :uniqueness => true validates : image_url, :format => { :with => %r{\.(gif|jpg|png)$}i, :message => 'must be a URL for GIF,JPG or PNG image.'}end
The validates method is a standard RailsVerifyBased on one or more attributes.VerifyOne or more model fields.
: Presence => true: the field value is not empty.
: Numericality =>{: greater_than_or_1__to => 0.01} the field value is a positive value and greater than 0.01.
: Uniqueness => true: this field cannot be repeated.
: Format =>{: with =>% r {\. (gif | jpg | png) $} I} Control Field format
Running effect:
1. The field title, description, and image URL are not empty.
2. The price is a valid number and should not be less than 0.01
3. The title is unique among all products.
4. The image URL looks valid.