Culture in rails verification information

Source: Internet
Author: User

All those who have used rails know that rails has powerful verification functions. You only need to define the business logic to automatically generate verification at the presentation layer. The product is verified in the agile web development book:

The problem is: all the text in this figure is in English !! OK. Let's turn it into Chinese.

Step 1: Compile the prompt information of each project in Chinese.Code:

Validates_presence_of: Title
Validates_presence_of: Description
Validates_presence_of: image_ur
Validates_numericality_of: Price
Validates_uniqueness_of: Title
Validates_format_of: image_url,
: With => % R {\. (GIF | JPG | PNG) $} I

Changed:

Validates_presence_of: title,: Message => "Enter the product name. "
Validates_presence_of: Description,: Message => "Enter the product description. "
Validates_presence_of: image_url,: Message => "Enter the product image to connect. "
Validates_numericality_of: Price,: Message => "enter the correct product price. "
Validates_uniqueness_of: title,: Message => "the product name already exists. "
Validates_format_of: image_url,
: With => % R {\. (GIF | JPG | PNG) $} I,
: Message => "the product image must be a connection to a GIF, JPG, or PNG image. "

Each validates operation can have a message to indicate an error message indicating verification failure. If you change it to a Chinese character, it will be OK! Of course, there are also automated methods to eliminate duplicates such as "Please input. However, words such as title, description, and image_url cannot be automated. Therefore, the above method works better. Below is:

Oh, day, what fuck is that ?!! It doesn't matter. This is because of the format of rails's automatic statement verification.

Add this method to helper/application_helper.rb:

Def error_messages_for (object_name, Options = {})
Options = options. symbolize_keys
Object = instance_variable_get ("# {object_name }")
Unless object. errors. Empty?
Error_lis = []
Object. errors. Each {| key, MSG | error_lis <content_tag ("Li", MSG )}
Content_tag ("Div", content_tag (options [: header_tag] | "h2", "occurrence # {object. errors. count} errors ") + content_tag (" Ul ", error_lis)," ID "=> options [: Id] |" errorexplanation "," class "=> options [: class] | "errorexplanation ")
End
End

This effect will appear:

OK. The ticket is closed. As for the function, make sure you can understand it at once!

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.