Ruby practices-simple database operations

Source: Internet
Author: User

Development Environment:

RUBY: 1.9.1

Rails: 2.3.5

RAKE: 0.8.7

Rack: 1.0.1

MySQL: 5.0.9

Ruby-MYSQL: mysql-2.8.1-x86-mswin

IDE: rubymine2.0.1

 

Database preparation:

Database: dbdevelopment

User: Crystal

Password: Crystal

 

1. Create the ruby project rortest

 

2. Modify database. yml

Only the development environment database is enabled here. modify the configuration file as follows:

Development: <br/> adapter: mysql <br/> encoding: utf8 <br/> reconnect: false <br/> Database: dbdevelopment <br/> pool: 5 <br/> Username: Crystal <br/> password: Crystal <br/> HOST: localhost

 

3. Run Scaffold

Rortest-> New-> scaffold running parameter: product title: String Description: String price: integer

 

 

After the program is successfully run, the directory and files (including model, help, and view) created during the running process will be output in the console)

 

Iv. Reverse generation of database tables

Find a file similar to create_products.rb in dB/migrate, right-click Run

 

 

After successful running, schema_migrations and products data tables are generated in the corresponding database.

 

5. Modify routes. Rb

Add a ing rule to routes. Rb

Map. connect '/products',: controller => "product",: Action => "Index" <br/> map. connect ': Controller/: Action/: id' <br/> map. connect ': Controller/: Action/: ID.: format'

 

6. Start the server and run (http: // localhost: 3000/products)

 

Note: You can test the show, edit, destroy, and new product functions by yourself. the following code is added to RB to verify the fields in the product. If you are interested, you can test them.

 

Class product <activerecord: Base
# Verification required
Validates_presence_of: title,: Price
End

 Class product <activerecord: Base <br/> # required for verification. The message is displayed or not, as shown in the following figure <br/> # validates_presence_of: title ,: price <br/> validates_presence_of: title,: Message => ": Please input title" <br/> validates_presence_of: Price,: Message => ": please input price "<br/> end

 

Related Article

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.