Ruby on Rails (six)

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

1, The data in the Ruby validation

(1)Verification of non text field is empty

For example:

validates:title, :description, :image_url, :presence=>true

The validates is a standard Rails validator. It will be to verify one or more model field according to one or more conditions. : presence:=> true let the verifier to verify each field named exists, and its content is not empty.

The code in the title, description, image_url are the data field to verify the name (field model).

(2)Verify whether positive

Use the numericality option, it can judge whether the input is a valid number, in addition, to send a greater_than_or_equal_to 0 digital input, to judge whether the content of greater than 0

For example:

validates:prices, : numericality=>{:greater_than_or_equal_to=>0}

Verify that the prices field is positive.

(3)Uniqueness verification data

For example: validates:title , :uniqueness=>true

Prove the uniqueness of title field data

(4)Verify the validity of the URL.

For the sake of convenience, here only to verify whether the end with GIF, JPG, PNG, format can be used to achieve this requirement, judge and regular expression matching.

validates:image_url, :format=>{

:with=> %r{ \.(gif|jpg|png)$}i,

:message=> "must be URL for GIF, JPG or PNGimage"

}

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Elaine at December 19, 2013 - 5:14 AM