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