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
Ruby on Rails Tutorial 第二章 之 微博资源的更多相关文章
- Ruby on Rails Tutorial 第二章 之 用户资源&MVC&REST
说明:用户资源包括用户数据模型和这个模型相关的Web页面. 1.用户数据模型如下: 2.使用Rails内置的脚手架生成用户资源中,执行如下所示命令: $ rails generate scaffold ...
- Ruby on Rails Tutorial 第二章 之 toy_app项目搭建
(第一章小结) 第一步:生成项目骨架 $ rails _4.1.6_ new toy_app 第二步:修改Gemfile 第三步:安装gem $ bundle install --without pr ...
- Ruby on Rails Tutorial 第一章 之 简介
1.目标:掌握MVC和REST.生成器.迁移.路由.嵌入式Ruby 本书涉及Rails,Ruby语言,Rails默认使用的测试框架(MiniTest),Unix命令行,HTML,CSS,少量的Java ...
- Ruby on Rails Tutorial 第一章 之 搭建开发环境
云端开发环境,Cloud9(https://ide.c9.io/).这个开发环境预先安装好了Rails开发所需要的大多数软件,包括Ruby.RubyGems和Git,需要自己安装Rails. 1.安装 ...
- Ruby on Rails Tutorial 第一章笔记
搭建开发环境 作者介绍了 Cloud9\ Coding.net 这样的云端开发环境 安装 Rails 1. 新建 rails 应用 首先,调用 rails new 命令创建一个新的 Rails 应用, ...
- Ruby on Rails Tutorial 第一章 之 Heroku部署
1.目的:用Heroku将开发环境部署到生产环境中.Heroku专门用于部署Rails和其他Web应用,部署Rails应用的过程非常简单——只要源码纳入Git版本控制系统就好. 2.搭建Heroku部 ...
- Ruby on Rails Tutorial 第一章 之 Git项目管理
1.安装和设置 (1)git的安装(略) (2)初始化设置 $ git config --global user.name "LihuaSun" $ git config --gl ...
- Ruby on Rails Tutorial读书笔记-1
只是怕忘了命令,全部撸一次,记个大概.. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 安装Ruby之前,先要安装RVM: curl -L https://get.rvm.io | bas ...
- Ruby on Rails Tutorial 第2版 学习笔记
Ruby on Rails Tutorial 第2版 在线阅读:http://railstutorial-china.org/ 英文版:http://ruby.railstutorial.org/ru ...
随机推荐
- The Automated Testing Handbook 自动化测试手册简介
Learn what works, what doesn't and why. The Automated Testing Handbook is a practical blueprint for ...
- Tkinter教程之Listbox篇
本文转载自:http://blog.csdn.net/jcodeer/article/details/1811310 #Tkinter教程之Listbox篇#Listbox为列表框控件,它可以包含一个 ...
- C# 字符串格式
1.数字补零输出字符串 String.Format("{0:D6}",123); 2.字符串靠左输出6位 String.Format("{0,-6}",&quo ...
- 设置mysql 在mac中的环境变量
在mac os的用户目录下有一个隐藏文件.bash_profile,编辑它就可以完成环境变量的创建. 比如要将mysql的运行目录加到环境变量中,可以在.bash_profile中新增如下一行: ex ...
- 用Gitolite 构建 Git 服务器
转载 Gitolite 构建 Git 服务器 作者: 北京群英汇信息技术有限公司 网址: http://www.ossxp.com/ 版本: 0.1-1 日期: 2010-10-07 14:52:19 ...
- IOS下arm64汇编疑问
之前所有关于32位下的纯汇编.s代码,在编译arm64的时候,很多错误,不得已只能用C代码. 但是arm_neon.h内部类C接口的汇编,基本上没有问题.不敢完全保证,还有待确认.关于arm64位的汇 ...
- work8
使用裸指针: #include <iostream>#include <memory>#include <stdio.h>#include <cstring& ...
- C#中的ref和out的区别
转载原地址 http://www.cnblogs.com/gjahead/archive/2008/02/28/1084871.html ref和out的区别在C# 中,既可以通过值也可以通过引用传递 ...
- Light oj 1197 - Help Hanzo (素数筛技巧)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1197 给你a和b求a到b之间的素数个数. 先在小区间素数筛,大区间就用类似素数筛的想法 ...
- ObjC-观察者模式
观察者模式是设计模式的一种,又称为发布者/订阅者模式,其定义了一种一对多的关系,多个观察者可以监听一个对象.当该对象的状态发生改变时,会通知所有的观察者,观察者会自己进行更新. 观察者模式能够将观察者 ...