Ruby on Rails Tutorial chapter II Weibo resources

Source: Internet
Author: User
Tags ruby on rails

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

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.