1. The microblog model is as follows:
2, the creation of Weibo resources, the command is as follows:
$ rails Generate scaffold micropost content:text User_id:integer #生成微博资源
$ bundle EXEC Rake db:migrate #执行迁移, update the database, use the new data model
3, the content of micro-blog restrictions
(1) Limit the length of Weibo
In the APP/MODELS/MICROPOST.RB
class Micropost <activerecord::base validates:content, length:{maximum: }end
(2) Weibo cannot be empty
class Micropost <activerecord::base validates:content, length:{maximum: presence:trueend
4, a user has a number of micro-blog
In APP/MODELS/USER.RB
class User <activerecord::base Has_many:micropostsend
In APP/MODELS/MICROPOST.RB
class Micropost <activerecord::base Belongs_to:user validates:content, length:{maximum: }end
Verifying the relationship between user and Micropost
$ rails Console>> first_user = user.first>> first_user.microposts>> Micropost = First_user.microposts.first>> micropost.user>> exit
5. The inheritance relationships in Userscontroller and Micropostscontroller are as follows:
6. Deploy the Application
-"Finish toy app"$ git push$ git push heroku$ heroku run rake db:migrate
Ruby on Rails Tutorial chapter II Weibo resources