ArticleDirectory
- 1. Create a model named blog
During website development, databases often change, and repeated input of some test data is very troublesome. In this case, you can use rake DB: seed to initialize some data, which will call dB/seeds. RB file, instance description.
1. Create a model named blog
Run rails G model Blog content: string created: datetime title: Text
A new app/models/blog. RB will be created with the following content:
Class blog <activerecord: Base
Attr_accessible: content,: created,: Title
End
2. Open the DB/seeds. RB file and add the following statement:
# Encoding: UTF-8
T = time. Now
# Blog. Create (: Title => "first Chinese version",: created => T,: content => "first Chinese version ")
# The following error will be reported for rake DB: Seed during initialization:
# Rake aborted!
# Can't mass-assign protected attributes: title, created, content
# Blog. Create (: Title => "first Chinese version",: created => T,: content => "first Chinese version ")
Blog. Create ({: Title => "first Chinese version",: created => T,: content => "first Chinese version "})
Blog. Create ({: Title => "Chinese Version 2",: created => T,: content => "People's Republic of China "})
# If time is required, write it directly without adding # {}, which indicates a string.
# Blog. Create ({: Title => "third edition of Chinese",: created =>#{ time. Now},: content => "People's Republic of China "})
Blog. Create ({: Title => "third edition of Chinese",: created => time. Now,: content => "People's Republic of China "})
3. Run rake DB: seed.
Run rails S and enter http: // 192.168.1.107: 3000/blogs in the browser.
:
The data in seeds. Rb is saved to the database file dB/development. sqlite3.