zoukankan      html  css  js  c++  java
  • Ruby on Rails Tutorial 第二章 之 微博资源

    1、微博模型如下图所示:

    2、创建微博资源,命令如下:

    $ rails generate scaffold Micropost content:text user_id:integer  #生成微博资源

    $ bundle exec rake db:migrate   #执行迁移,更新数据库,使用新建的数据模型

    3、微博内容的限制

    (1)限制微博长度

    在app/models/micropost.rb中

    class Micropost <ActiveRecord::Base
      validates :content, length:{ maximum:140 }
    end

    (2)微博不能为空

    class Micropost <ActiveRecord::Base
      validates :content, length:{ maximum:140 } ,presence: true
    end

    4、一个用户拥有多篇微博

    app/models/user.rb中

    class User <ActiveRecord::Base
      has_many :microposts
    end

    app/models/micropost.rb中

    class Micropost <ActiveRecord::Base
      belongs_to :user
      validates :content, length:{ maximum:140 }
    end

    验证user与micropost之间的关系

    $ rails console
    >> first_user = User.first
    >> first_user.microposts
    >> micropost = first_user.microposts.first
    >> micropost.user
    >> exit

    5、UsersController和MicropostsController中的继承关系如下图所示:

    6、部署应用

    $ git status 
    $ git add -A
    $ git commit -m "Finish toy app"
    $ git push
    $ git push heroku
    $ heroku run rake db:migrate
  • 相关阅读:
    hard example mining(困难样本挖掘)
    Docker
    cmake使用教程
    CFENet: An Accurate and Efficient Single-Shot Object Detector for Autonomous Driving
    Week1
    To-Read List
    《人性的弱点》读书笔记及读后感
    总结计划:2018:上半年——毕业前
    TCP网路程序设计
    Linux 串口驱动设计二
  • 原文地址:https://www.cnblogs.com/sunflower627/p/4623131.html
Copyright © 2011-2022 走看看